Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- const int ledOnPin = 3;
- const int ledOffPin = 2;
- const int powerButton = 13;
- const int speedButton = 12;
- const int red_pin = 5;
- const int green_pin = 6;
- const int blue_pin = 7;
- const int remote_pin = 4;
- int circuitOn = LOW;
- int circuitOff = HIGH;
- int buttonCurrent;
- int buttonPrevious = LOW;
- int speedButtonCurrent;
- int speedCurrent = 0;
- int pos;
- int remoteCurrent;
- int remotePrevious = 0;
- Servo myservo;
- void setup () {
- Serial.begin(9600);
- pinMode(powerButton, INPUT);
- pinMode(ledOnPin, OUTPUT);
- pinMode(ledOffPin, OUTPUT);
- pinMode(speedButton, INPUT);
- pinMode(red_pin, OUTPUT);
- pinMode(green_pin, OUTPUT);
- pinMode(blue_pin, OUTPUT);
- myservo.attach(9);
- myservo.write(10);
- }
- void loop ()
- {
- handle_buttons();
- digitalWrite(ledOnPin, circuitOn);
- digitalWrite(ledOffPin, circuitOff);
- buttonPrevious = buttonCurrent;
- speedButtonCurrent = digitalRead(speedButton);
- if (speedButtonCurrent == HIGH && circuitOn == HIGH)
- {
- onButtonPress();
- }
- servoDelay();
- if (speedCurrent == 0)
- {
- setColor(0, 0, 0);
- }
- else if (speedCurrent == 1)
- {
- setColor(255, 0, 0);
- }
- else if (speedCurrent == 2)
- {
- setColor(255, 100, 0);
- }
- else if (speedCurrent == 3)
- {
- setColor(0, 255, 0);
- }
- my_delay(20);
- }
- void setColor(int red, int green, int blue)
- {
- analogWrite(red_pin, red);
- analogWrite(green_pin, green);
- analogWrite(blue_pin, blue);
- }
- void servoDelay ()
- {
- if (circuitOn == HIGH)
- {
- for (pos = 30; pos <= 60; pos += 30) { // goes from 0 degrees to 180 degrees
- // in steps of 1 degree
- myservo.write(pos); // tell servo to go to position in variable 'pos'
- my_delay(250 / (speedCurrent)); // waits 15ms for the servo to reach the position
- }
- for (pos = 60; pos >= 30; pos -= 30) { // goes from 180 degrees to 0 degrees
- myservo.write(pos); // tell servo to go to position in variable 'pos'
- my_delay(250 / (speedCurrent)); // waits 15ms for the servo to reach the position
- }
- }
- }
- void onButtonPress ()
- {
- speedCurrent = speedCurrent + 1;
- my_delay(500);
- if (speedCurrent > 3)
- {
- speedCurrent = 1;
- my_delay(500);
- }
- }
- void handle_buttons()
- {
- buttonCurrent = digitalRead(powerButton);
- if (buttonCurrent == HIGH && buttonPrevious == LOW)
- {
- if (circuitOn == HIGH)
- {
- speedCurrent = 0;
- circuitOn = LOW;
- circuitOff = HIGH;
- }
- else if (buttonCurrent == HIGH)
- {
- setColor(255, 0, 0);
- speedCurrent = 1;
- circuitOn = HIGH;
- circuitOff = LOW;
- }
- }
- buttonCurrent = digitalRead(powerButton);
- }
- void my_delay(unsigned long s) {
- for (unsigned long x=0; x < s; x++) {
- delay(1);
- handle_buttons();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement