Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(12, 11, 16, 17, 18, 19);
- const int photoPin = A1;
- int photoVal = 0;
- const int controlPin1 = 2; // connected to pin 7 on the H-bridge
- const int controlPin2 = 3; // connected to pin 2 on the H-bridge
- const int enablePin = 9; // connected to pin 1 on the H-bridge
- const int directionSwitchPin = 4; // connected to the switch for direction
- const int onOffSwitchStateSwitchPin = 5; // connected to the switch for turning the motor on and off
- const int potPin = A0; // connected to the potentiometer's output
- // create some variables to hold values from your inputs
- int onOffSwitchState = 0; // current state of the On/Off switch
- int previousOnOffSwitchState = 0; // previous position of the on/off switch
- int directionSwitchState = 0; // current state of the direction switch
- int previousDirectionSwitchState = 0; // previous state of the direction switch
- int motorEnabled = 0; // Turns the motor on/off
- int motorSpeed = 0; // speed of the motor
- int motorDirection = 1; // current direction of the motor
- void setup() {
- // put your setup code here, to run once:
- Serial.print (9600);
- lcd.begin (16, 2);
- for (int i = 0; i <= 15; i++)
- {
- lcd.setCursor (i, 0);
- lcd.print ("James");
- delay(300);
- lcd.clear();
- }
- }
- void loop() {
- // put your main code here, to run repeatedly:
- photoVal = analogRead (photoPin);
- lcd.print ("the value is");
- lcd.setCursor(6, 1);
- lcd.print (photoVal);
- delay(50);
- lcd.clear();
- onOffSwitchState = digitalRead(onOffSwitchStateSwitchPin);
- delay(1);
- Serial.println(onOffSwitchState);
- // read the value of the direction switch
- directionSwitchState = digitalRead(directionSwitchPin);
- Serial.println(directionSwitchState);
- // read the value of the pot and divide by 4 to get
- // a value that can be used for PWM
- motorSpeed = analogRead(potPin);
- motorSpeed = map (motorSpeed, 0, 1023, 0, 255);
- motorSpeed = constrain (motorSpeed, 0, 255);
- // if the on/off button changed state since the last loop()
- if (onOffSwitchState != previousOnOffSwitchState) {
- // change the value of motorEnabled if pressed
- if (onOffSwitchState == HIGH) {
- motorEnabled = !motorEnabled;
- }
- }
- // if the direction button changed state since the last loop()
- if (directionSwitchState != previousDirectionSwitchState) {
- // change the value of motorDirection if pressed
- if (directionSwitchState == HIGH) {
- motorDirection = !motorDirection;
- }
- }
- // change the direction the motor spins by talking
- // to the control pins on the H-Bridge
- if (motorDirection == 1) {
- digitalWrite(controlPin1, HIGH);
- digitalWrite(controlPin2, LOW);
- } else {
- digitalWrite(controlPin1, LOW);
- digitalWrite(controlPin2, HIGH);
- }
- // if the motor is supposed to be on
- if (motorEnabled == 1) {
- // PWM the enable pin to vary the speed
- analogWrite(enablePin, motorSpeed);
- } else { // if the motor is not supposed to be on
- //turn the motor off
- analogWrite(enablePin, 0);
- }
- // save the current On/Offswitch state as the previous
- previousDirectionSwitchState = directionSwitchState;
- // save the current switch state as the previous
- previousOnOffSwitchState = onOffSwitchState;
- lcd.clear();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement