Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //version ovs15 с фонариком на красную и SOS на синюю кнопку
- int inputPinRed = 2;
- int inputPinGreen = 3;
- int inputPinBlue = 4;
- int outputPinRed = 9;
- int outputPinGreen = 10;
- int outputPinBlue = 11;
- int dutyCycleRed = 0;
- int dutyCycleGreen = 0;
- int dutyCycleBlue = 0;
- int upDutyCycleRed = 0;
- int upDutyCycleGreen = 0;
- int upDutyCycleBlue = 0;
- int buttonPressedRed = 0;
- int buttonPressedGreen = 0;
- int buttonPressedBlue = 0;
- void setup()
- {
- pinMode(inputPinRed, INPUT);
- pinMode(inputPinGreen, INPUT);
- pinMode(inputPinBlue, INPUT);
- pinMode(outputPinRed, OUTPUT);
- pinMode(outputPinGreen, OUTPUT);
- pinMode(outputPinBlue, OUTPUT);
- if (digitalRead(inputPinRed) == LOW) lantern();
- else if (digitalRead(inputPinBlue) == LOW) sos();
- else normalMode();
- }
- void normalMode()
- {
- lightsOff();
- for(;;)
- {
- checkButtons();
- changeDutyCycles();
- delay(200);
- }
- }
- void sos()
- {
- lightsOff();
- for(;;)
- {
- blink(300);
- blink(800);
- blink(300);
- delay(1000);
- }
- }
- void blink(int length)
- {
- for(int i = 0; i < 3; i++)
- {
- delay(300);
- lightsOn();
- delay(length);
- lightsOff();
- }
- }
- void lightsOn()
- {
- analogWrite(outputPinRed, 0);
- analogWrite(outputPinBlue, 0);
- analogWrite(outputPinGreen, 0);
- }
- void lightsOff()
- {
- analogWrite(outputPinRed, 255);
- analogWrite(outputPinBlue, 255);
- analogWrite(outputPinGreen, 255);
- }
- void lantern()
- {
- for(;;);
- }
- void loop()
- {
- }
- void checkButtons()
- {
- checkButton(&buttonPressedRed, &inputPinRed, &upDutyCycleRed);
- checkButton(&buttonPressedBlue, &inputPinBlue, &upDutyCycleBlue);
- checkButton(&buttonPressedGreen, &inputPinGreen, &upDutyCycleGreen);
- }
- void checkButton(int* buttonPressed, int* inputPin, int* upDutyCycle)
- {
- int buttonPressedBefore = *buttonPressed;
- *buttonPressed = digitalRead(*inputPin) == LOW;
- if(!buttonPressedBefore && *buttonPressed)
- *upDutyCycle = !*upDutyCycle;
- }
- void changeDutyCycles()
- {
- if (buttonPressedRed) changeDutyCycle(&dutyCycleRed, &upDutyCycleRed, &outputPinRed);
- if (buttonPressedBlue) changeDutyCycle(&dutyCycleBlue, &upDutyCycleBlue, &outputPinBlue);
- if (buttonPressedGreen) changeDutyCycle(&dutyCycleGreen, &upDutyCycleGreen, &outputPinGreen);
- }
- void changeDutyCycle(int* dutyCycle, int* upDutyCycle, int* outputPin)
- {
- *dutyCycle += *upDutyCycle ? 4 : -4;
- if (*dutyCycle > 255) *dutyCycle = 255;
- if (*dutyCycle < 0) *dutyCycle = 0;
- analogWrite(*outputPin, 255-*dutyCycle);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement