Advertisement
mongerr

Combining Programs

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