 Hello,

I am trying to make a LED blink when a button on a 3X3 keypad is pressed using millis(). This is what I wrote for the blinking part:

`````` unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval ) {
previousMillis = currentMillis;

if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
if(rowtwoState == HIGH && coloneState == HIGH){ //check to see if button is pressed
digitalWrite(ledtwo, ledState);
}
}
``````

This is not working so I am wondering if someone could tell me what I am doing wrong? My prof wants us to check if the row is powered and if the column is powered in our code so that is why i am writing it this way instead of using the keypad library way.

Here is my full code, everything else works except for the blinking part.

``````const int rowtwo = 3;
const int rowfour = 2;
const int colone = 6;
const int coltwo = 5;
const int colthree = 4;
const int ledtwo = 10;
const int ledone = 12;

int rowtwoState = 0;
int rowfourState = 0;
int coloneState = 0;
int coltwoState = 0;
int colthreeState = 0;

int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 1000;

void setup() {
Serial.begin(9600);
pinMode(rowtwo, OUTPUT);
digitalWrite(rowtwo, HIGH);
pinMode(rowfour, OUTPUT);
digitalWrite(rowfour, LOW);
pinMode(ledone, OUTPUT);
digitalWrite(ledone, LOW);
pinMode(ledtwo, OUTPUT);
digitalWrite(ledtwo, LOW);

pinMode(colone, INPUT);
digitalWrite(colone, LOW);
pinMode(coltwo, INPUT);
digitalWrite(coltwo, LOW);
pinMode(colthree, INPUT);
digitalWrite(colthree, LOW);
}

void loop() {

unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval && ) {
previousMillis = currentMillis;

if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
if(rowtwoState == HIGH && coloneState == HIGH){
digitalWrite(ledtwo, ledState);
// digitalWrite(rowfour, HIGH);
//digitalWrite(rowtwo, LOW);
}
}
if(rowtwoState == HIGH && colthreeState == HIGH){
digitalWrite(ledtwo, HIGH);
digitalWrite(rowfour, HIGH);
digitalWrite(rowtwo, LOW);
}
if(rowtwoState == HIGH && coltwoState == HIGH){
digitalWrite(ledone, HIGH);
delay(1000);
digitalWrite(ledone, LOW);
delay(1000);
digitalWrite(ledone, HIGH);
delay(1000);
digitalWrite(ledone, LOW);
delay(1000);
digitalWrite(ledone, HIGH);
digitalWrite(rowtwo, LOW);
digitalWrite(rowfour, HIGH);
}
if(rowfourState == HIGH && coloneState == HIGH){
digitalWrite(ledone, LOW);
digitalWrite(ledtwo, LOW);
digitalWrite(rowfour, LOW);
digitalWrite(rowtwo, HIGH);
}
}
``````

Thanks

``````    if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
``````

could be replaced by

``````   ledState = !ledState;
``````
``````  if (currentMillis - previousMillis >= interval && ) {
``````

And what?

It's silly to say that the blinking doesn't work when you have code that doesn't even compile.

My bad, I forgot to take out the &&. Also the ledState = !ledState; seemed to work only if I hold the button down. How would I make it so it blinks after being pressed so I wouldn't have to hold it down?

``````bool doBlinking = false ;
``````

Set doBlinking to true when the button is pressed. Do the blinking if (and only if) doBlinking is true.