Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* DC motor and lcd with photopin
- * version 1
- * Sophia Brisbon 6/19/17
- *
- */
- const int controlPin1 = 2;
- const int controlPin2 = 3;
- const int enablePin = 9;
- const int directionSwitchPin = 4;
- const int onOffSwitchStateSwitchPin = 5;
- const int potPin = A0;
- int onOffSwitchState = 0;
- int previousOnOffSwitchState = 0;
- int directionSwitchState = 0;
- int previousDirectionSwitchState = 0;
- int motorEnabled = 0;
- int motorSpeed = 0;
- int motorDirection = 1;
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(12,11,7,8, 13, 10);
- const int photoPin = A2;
- int photoVal;
- void setup()
- {
- pinMode(directionSwitchPin, INPUT);
- pinMode(onOffSwitchStateSwitchPin, INPUT);
- pinMode(controlPin1, OUTPUT);
- pinMode(potPin, INPUT);
- pinMode(enablePin, OUTPUT);
- digitalWrite(enablePin, LOW);
- pinMode(photoPin, INPUT);
- Serial.begin(9600);
- for(int counter = 0; counter < 11; counter++)
- {
- lcd.begin(16,2);
- lcd.print("My name is");
- lcd.setCursor(counter, 1);
- lcd.print("Sophia");
- delay(300);
- lcd.clear();
- }
- Serial.begin(9600);
- }
- void loop()
- {
- photoVal=analogRead(photoPin);
- lcd.print("The value is:");
- lcd.setCursor(6,1);
- lcd.print(photoVal);
- delay(100);
- lcd.clear();
- onOffSwitchState = digitalRead(onOffSwitchStateSwitchPin);
- //Serial.print(onOffSwitchState);
- delay(1);
- directionSwitchState = digitalRead(directionSwitchPin);
- //Serial.print(directionSwitchState);
- motorSpeed = map(analogRead(potPin), 0, 1023, 0, 255);
- motorSpeed = constrain(motorSpeed,0,255);
- if(onOffSwitchState != previousOnOffSwitchState)
- { if(onOffSwitchState == HIGH)
- { motorEnabled = !motorEnabled;
- }
- }
- if(directionSwitchState != previousDirectionSwitchState)
- { if(directionSwitchState == HIGH)
- { motorDirection = !motorDirection;
- }
- }
- if(motorDirection ==1)
- { digitalWrite(controlPin1, HIGH);
- digitalWrite(controlPin2, LOW);
- }
- else
- { digitalWrite(controlPin1, LOW);
- digitalWrite(controlPin2, HIGH);
- } if(motorEnabled ==1)
- { analogWrite(enablePin, motorSpeed);
- }
- else
- { analogWrite(enablePin, 0);
- }
- previousDirectionSwitchState = directionSwitchState;
- previousOnOffSwitchState = onOffSwitchState;
- Serial.print(motorEnabled);
- Serial.print(" ");
- Serial.println(motorDirection);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement