Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. const int ledOnPin = 3;
  4. const int ledOffPin = 2;
  5. const int powerButton = 13;
  6. const int speedButton = 12;
  7. const int red_pin = 5;
  8. const int green_pin = 6;
  9. const int blue_pin = 7;
  10. const int remote_pin = 4;
  11.  
  12. int circuitOn = LOW;
  13. int circuitOff = HIGH;
  14. int buttonCurrent;
  15. int buttonPrevious = LOW;
  16. int speedButtonCurrent;
  17. int speedCurrent = 0;
  18. int pos;
  19. int remoteCurrent;
  20. int remotePrevious = 0;
  21.  
  22. Servo myservo;
  23.  
  24. void setup () {
  25.  
  26. Serial.begin(9600);
  27.  
  28. pinMode(powerButton, INPUT);
  29. pinMode(ledOnPin, OUTPUT);
  30. pinMode(ledOffPin, OUTPUT);
  31. pinMode(speedButton, INPUT);
  32. pinMode(red_pin, OUTPUT);
  33. pinMode(green_pin, OUTPUT);
  34. pinMode(blue_pin, OUTPUT);
  35.  
  36. myservo.attach(9);
  37. myservo.write(10);
  38.  
  39. }
  40.  
  41. void loop ()
  42. {
  43.  
  44. handle_buttons();
  45.  
  46. digitalWrite(ledOnPin, circuitOn);
  47. digitalWrite(ledOffPin, circuitOff);
  48.  
  49. buttonPrevious = buttonCurrent;
  50.  
  51. speedButtonCurrent = digitalRead(speedButton);
  52.  
  53. if (speedButtonCurrent == HIGH && circuitOn == HIGH)
  54. {
  55. onButtonPress();
  56. }
  57.  
  58.  
  59. servoDelay();
  60.  
  61.  
  62. if (speedCurrent == 0)
  63. {
  64. setColor(0, 0, 0);
  65.  
  66. }
  67. else if (speedCurrent == 1)
  68. {
  69. setColor(255, 0, 0);
  70. }
  71. else if (speedCurrent == 2)
  72. {
  73. setColor(255, 100, 0);
  74. }
  75. else if (speedCurrent == 3)
  76. {
  77. setColor(0, 255, 0);
  78. }
  79.  
  80. my_delay(20);
  81. }
  82.  
  83.  
  84. void setColor(int red, int green, int blue)
  85. {
  86. analogWrite(red_pin, red);
  87. analogWrite(green_pin, green);
  88. analogWrite(blue_pin, blue);
  89. }
  90.  
  91. void servoDelay ()
  92. {
  93. if (circuitOn == HIGH)
  94. {
  95. for (pos = 30; pos <= 60; pos += 30) { // goes from 0 degrees to 180 degrees
  96. // in steps of 1 degree
  97. myservo.write(pos); // tell servo to go to position in variable 'pos'
  98. my_delay(250 / (speedCurrent)); // waits 15ms for the servo to reach the position
  99. }
  100. for (pos = 60; pos >= 30; pos -= 30) { // goes from 180 degrees to 0 degrees
  101. myservo.write(pos); // tell servo to go to position in variable 'pos'
  102. my_delay(250 / (speedCurrent)); // waits 15ms for the servo to reach the position
  103. }
  104. }
  105. }
  106.  
  107. void onButtonPress ()
  108. {
  109.  
  110. speedCurrent = speedCurrent + 1;
  111. my_delay(500);
  112.  
  113. if (speedCurrent > 3)
  114. {
  115. speedCurrent = 1;
  116. my_delay(500);
  117. }
  118. }
  119.  
  120. void handle_buttons()
  121. {
  122.  
  123. buttonCurrent = digitalRead(powerButton);
  124.  
  125.  
  126. if (buttonCurrent == HIGH && buttonPrevious == LOW)
  127. {
  128. if (circuitOn == HIGH)
  129. {
  130. speedCurrent = 0;
  131. circuitOn = LOW;
  132. circuitOff = HIGH;
  133.  
  134.  
  135.  
  136. }
  137. else if (buttonCurrent == HIGH)
  138. {
  139. setColor(255, 0, 0);
  140. speedCurrent = 1;
  141. circuitOn = HIGH;
  142. circuitOff = LOW;
  143.  
  144. }
  145. }
  146. buttonCurrent = digitalRead(powerButton);
  147. }
  148.  
  149. void my_delay(unsigned long s) {
  150. for (unsigned long x=0; x < s; x++) {
  151. delay(1);
  152. handle_buttons();
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement