Advertisement
brisbons19

combination code

Jun 26th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. /* DC motor and lcd with photopin
  2. * version 1
  3. * Sophia Brisbon 6/19/17
  4. *
  5. */
  6.  
  7. const int controlPin1 = 2;
  8. const int controlPin2 = 3;
  9. const int enablePin = 9;
  10. const int directionSwitchPin = 4;
  11. const int onOffSwitchStateSwitchPin = 5;
  12. const int potPin = A0;
  13. int onOffSwitchState = 0;
  14. int previousOnOffSwitchState = 0;
  15. int directionSwitchState = 0;
  16. int previousDirectionSwitchState = 0;
  17. int motorEnabled = 0;
  18. int motorSpeed = 0;
  19. int motorDirection = 1;
  20. #include <LiquidCrystal.h>
  21. LiquidCrystal lcd(12,11,7,8, 13, 10);
  22. const int photoPin = A2;
  23. int photoVal;
  24.  
  25. void setup()
  26. {
  27. pinMode(directionSwitchPin, INPUT);
  28. pinMode(onOffSwitchStateSwitchPin, INPUT);
  29. pinMode(controlPin1, OUTPUT);
  30. pinMode(potPin, INPUT);
  31. pinMode(enablePin, OUTPUT);
  32. digitalWrite(enablePin, LOW);
  33. pinMode(photoPin, INPUT);
  34. Serial.begin(9600);
  35. for(int counter = 0; counter < 11; counter++)
  36. {
  37. lcd.begin(16,2);
  38. lcd.print("My name is");
  39. lcd.setCursor(counter, 1);
  40. lcd.print("Sophia");
  41. delay(300);
  42. lcd.clear();
  43. }
  44. Serial.begin(9600);
  45.  
  46. }
  47.  
  48. void loop()
  49. {
  50. photoVal=analogRead(photoPin);
  51. lcd.print("The value is:");
  52. lcd.setCursor(6,1);
  53. lcd.print(photoVal);
  54. delay(100);
  55. lcd.clear();
  56. onOffSwitchState = digitalRead(onOffSwitchStateSwitchPin);
  57. //Serial.print(onOffSwitchState);
  58. delay(1);
  59. directionSwitchState = digitalRead(directionSwitchPin);
  60. //Serial.print(directionSwitchState);
  61. motorSpeed = map(analogRead(potPin), 0, 1023, 0, 255);
  62. motorSpeed = constrain(motorSpeed,0,255);
  63. if(onOffSwitchState != previousOnOffSwitchState)
  64. { if(onOffSwitchState == HIGH)
  65. { motorEnabled = !motorEnabled;
  66. }
  67. }
  68. if(directionSwitchState != previousDirectionSwitchState)
  69. { if(directionSwitchState == HIGH)
  70. { motorDirection = !motorDirection;
  71. }
  72. }
  73. if(motorDirection ==1)
  74. { digitalWrite(controlPin1, HIGH);
  75. digitalWrite(controlPin2, LOW);
  76. }
  77. else
  78. { digitalWrite(controlPin1, LOW);
  79. digitalWrite(controlPin2, HIGH);
  80. } if(motorEnabled ==1)
  81. { analogWrite(enablePin, motorSpeed);
  82. }
  83. else
  84. { analogWrite(enablePin, 0);
  85. }
  86. previousDirectionSwitchState = directionSwitchState;
  87. previousOnOffSwitchState = onOffSwitchState;
  88.  
  89.  
  90. Serial.print(motorEnabled);
  91. Serial.print(" ");
  92. Serial.println(motorDirection);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement