r/arduino•
marf843

Need Help with blink

Hello, I have programmed this code for an Arduino master and a slave. The master communicates trough i2c with the slave, that's all working fine. My problem is the blink. It isn't blinking at all. The blink is in an if statement I don't know if this is the problem. I hope somebody knows how to fix this.

That's the code:

// Wire Slave Receiver

#include <Wire.h> //include Library // define Led Pins #define LED1 3 //Pin 3 #define LED2 4 //Pin 4 #define LED3 5 //Pin 5

int ledState = LOW; // ledState used to set the LED unsigned long previousMillis = 0; // will store last time LED was updated const long interval = 500; // interval at which to blink (milliseconds)

void setup() { Wire.begin(4); // join i2c bus with address #4 Wire.onReceive(receiveEvent); // register event Serial.begin(9600); // start serial for output

//set Pin modes pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT); pinMode(LED3, OUTPUT); //intern pulldown digitalWrite(LED1, LOW); digitalWrite(LED2, LOW); digitalWrite(LED3, LOW);

}

void loop() { unsigned long currentMillis = millis(); delay(100); }

// function that executes whenever data is received from master // this function is registered as an event, see setup() void receiveEvent(int howMany) { while (1 < Wire.available()) // loop through all but the last {

char c = Wire.read(); // receive byte as a character
Serial.print(c, BIN);     // print the character as Binary

} int x = Wire.read(); // receive byte as an integer Serial.println(x, BIN); // print the integer as Binary

if (x == 1) { //turn LED1 on digitalWrite(LED2, LOW); digitalWrite(LED3, LOW); digitalWrite(LED1, HIGH); Serial.println("LED1 on"); } if (x == 2) { //blink LED1 unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; if (ledState == LOW) { ledState = HIGH; } else { ledState = LOW; } digitalWrite(LED1, ledState); } }

if (x == 3) { //turn LED2 on digitalWrite(LED1, LOW); digitalWrite(LED2, HIGH); delay(100); } if (x == 4) { //blink LED2 unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; if (ledState == LOW) { ledState = HIGH; } else { ledState = LOW; } digitalWrite(LED2, ledState); } } if (x == 5) { //turn LED3 on digitalWrite(LED2, LOW); digitalWrite(LED3, HIGH); } if (x == 6) { //blink LED3 unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; if (ledState == LOW) { ledState = HIGH; } else { ledState = LOW; } digitalWrite(LED3, ledState); } } if (x == 7) { //turn LED1 nad 2 on digitalWrite(LED3, LOW); digitalWrite(LED1, HIGH); digitalWrite(LED2, HIGH); } if (x == 8) { //Blink LED1 and 2 digitalWrite(LED1, LOW); digitalWrite(LED2, LOW); } if (x == 9) { //turn LED2 and 3 on digitalWrite(LED1, LOW); digitalWrite(LED2, LOW);

digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);

}

}

Posted Mar 12, 2020, 5:45 AM UTC

01
Loading comments…