Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int currentLedId = 0;
- int targetLedPowerState = 0;
- int currentLedPowerState = 0;
- float timer = 0;
- void setup() {
- pinMode(7, OUTPUT);
- pinMode(8, OUTPUT);
- pinMode(9, OUTPUT);
- Serial.begin(9600);
- }
- void loop() {
- if (Serial.available() > 0 ) {
- char incoming = Serial.read();
- Serial.flush();
- Serial.println(incoming);
- processIncomingMessage(incoming);
- }
- blink();
- }
- void blink() {
- timer += 0.01;
- if (timer > 1000) {
- Serial.println("1000/0.01 ticks is out");
- Serial.println("targetLedPowerState: " + String(targetLedPowerState));
- Serial.println("currentLedPowerState: " + String(currentLedPowerState));
- Serial.println("currentLedId: " + String(currentLedId));
- if (targetLedPowerState == 0) {
- turnLedsOff();
- } else if (targetLedPowerState == 1) {
- if (currentLedPowerState == 0) {
- turnLedOn(currentLedId);
- currentLedPowerState = 1;
- } else if (currentLedPowerState == 1) {
- turnLedsOff();
- currentLedPowerState = 0;
- }
- }
- timer = 0;
- }
- }
- void processIncomingMessage(char message) {
- switch (message) {
- case '0':
- currentLedId = 7;
- targetLedPowerState = 1;
- break;
- case '1':
- currentLedId = 8;
- targetLedPowerState = 1;
- break;
- case '2':
- currentLedId = 9;
- targetLedPowerState = 1;
- break;
- case '3':
- break;
- case 'f':
- currentLedId = 0;
- targetLedPowerState = 0;
- break;
- }
- }
- void turnLedOn (int ledPin) {
- turnLedsOff();
- digitalWrite(ledPin, 1);
- }
- void turnLedsOff () {
- digitalWrite(7, 0);
- digitalWrite(8, 0);
- digitalWrite(9, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement