#include <Servo.h>
int rPin = 13;
int gPin = 12;
int bPin = 11;
int bottonPin = 7;
int bottonLedPin = 2;
int fadePin = 8;
int servoPin = 6;
int R_ledState = LOW;
int G_ledState = LOW;
int B_ledState = LOW;
int Botton_ledState = LOW;
int bright = 0;
int pos = 0;
int posIncr = 1;
unsigned long currentMillis = 0;
unsigned long R_previousMillis = 0;
unsigned long G_previousMillis = 0;
unsigned long B_previousMillis = 0;
unsigned long Botton_previousMillis = 0;
unsigned long fade_previousMillis = 0;
unsigned long servo_previousMillis = 0;
int R_interval = 1000;
int G_interval = 500;
int B_interval = 100;
int Botton_interval = 10;
int fade_interval = 100;
////////////////servo
int servo_interval = 50;
Servo myservo;
void setup() {
pinMode(rPin, OUTPUT);
pinMode(gPin, OUTPUT);
pinMode(bPin, OUTPUT);
pinMode(bottonPin, INPUT_PULLUP);
pinMode(bottonLedPin, OUTPUT);
pinMode(fadePin, OUTPUT);
///////////////// servo
pinMode(servoPin, OUTPUT);
myservo.attach(servoPin);
}
void loop() {
Blink();
BottonPress();
fade();
servo();
}
void Blink() {
currentMillis = millis();
////////////////
if (currentMillis - R_previousMillis >= R_interval) {
R_previousMillis = currentMillis;
if (R_ledState == LOW) {
R_ledState = HIGH;
R_interval = 2000;
} else {
R_ledState = LOW;
R_interval = 500;
}
digitalWrite(rPin, R_ledState);
}
///////////////////
if (currentMillis - G_previousMillis >= G_interval) {
G_previousMillis = currentMillis;
if (G_ledState == LOW) {
G_ledState = HIGH;
G_interval = 2000;
} else {
G_ledState = LOW;
G_interval = 300;
}
digitalWrite(gPin, G_ledState);
}
//////////////////
if (currentMillis - B_previousMillis >= B_interval) {
B_previousMillis = currentMillis;
if (B_ledState == LOW) {
B_ledState = HIGH;
B_interval = 2000;
} else {
B_ledState = LOW;
B_interval = 100;
}
digitalWrite(bPin, B_ledState);
}
}
void BottonPress(){
currentMillis = millis();
if (currentMillis - Botton_previousMillis >= Botton_interval) {
Botton_previousMillis = currentMillis;
Botton_ledState = digitalRead(bottonPin);
if (Botton_ledState == LOW) {
Botton_ledState = HIGH;
} else {
Botton_ledState = LOW;
}
digitalWrite(bottonLedPin, Botton_ledState);
}
}
void fade(){
currentMillis = millis();
if (currentMillis - fade_previousMillis >= fade_interval) {
fade_previousMillis = currentMillis;
bright++;
analogWrite(fadePin,bright);
if (bright >= 255){
bright = 0;
}
}
}
void servo(){
currentMillis = millis();
if (currentMillis - servo_previousMillis >= servo_interval) {
servo_previousMillis = currentMillis;
pos += posIncr;
myservo.write(pos);
if (pos >=180 || pos <=0){
posIncr =- posIncr;
}
}
}