ambersy314

Untitled

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