Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Matt Vawrinek
- * 6/22/17
- * DC motor and LCD combined code
- */
- 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, 15, 16, 17, 18); // 15-18 are A1-A4, setting the pin numbers up, don't name GND and positive, start with side closest to the edge, from the top down from the LCD
- const int photoPin = A5;
- int photoVal = 0;
- void setup() {
- // put your setup code here, to run once:
- pinMode(potPin, INPUT);
- pinMode(directionSwitchPin, INPUT);
- pinMode(onOffSwitchStateSwitchPin, INPUT);
- pinMode(controlPin1, OUTPUT);
- pinMode(controlPin2, OUTPUT);
- pinMode(enablePin, OUTPUT);
- //pull the enable pin LOW to start
- digitalWrite(enablePin, LOW);
- Serial.begin(9600);
- // put your setup code here, to run once:
- pinMode(photoPin, INPUT);
- Serial.begin(9600);
- lcd.begin(16, 2);// dimensions
- ;
- for(int i = 0; i <= 13; i++) //first 12 numbers can fit on first row, ++ means go up by one
- {
- lcd.setCursor(i, 0);
- lcd.print("Matt");
- delay(300);
- lcd.clear();
- }
- lcd.clear();
- }
- void loop() {
- // put your main code here, to run repeatedly:
- onOffSwitchState =digitalRead(onOffSwitchStateSwitchPin);// read the value of the on/off switch
- delay(1);
- // Serial.println(onOffSwitchState);
- directionSwitchState = digitalRead(directionSwitchPin); //read the value of the direction
- Serial.println(directionSwitchState);
- motorSpeed = analogRead(potPin); //setting how fast the fan is spinning
- motorSpeed = map(motorSpeed, 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);
- }
- Serial.println(motorSpeed);
- previousDirectionSwitchState =
- directionSwitchState,
- previousOnOffSwitchState = onOffSwitchState;
- // put your main code here, to run repeatedly:
- photoVal = analogRead(photoPin); //read photoresistor value, since photoresistor is input and it its values are in a range, which is analog//
- lcd.print("The value is: ");
- lcd.setCursor(6, 1);
- lcd.print(photoVal);
- delay(100);
- lcd.clear();
- //for(int i = 0; i <= 13; i++) //first 12 numbers can fit on first row, ++ means go up by one
- {
- // lcd.setCursor(i, 0);
- // lcd.print("Matt");
- // delay(300);
- // lcd.clear();
- }
- // lcd.clear();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement