Use push button to change how led blink

Hi I am new to Arduino. I have some questions regarding my project.

I wrote several combinations of 0's and 1's to control how my LED blinks, once I press the push button, the pattern will change to the next combination. If I press again, it will change to the next again. I am thinking of using switch case to write. However, do I need to write, for example, digitalWrite(3, High), digitalWrite(3, low)under certain conditions for each case? or is it possible for me to do digitalWrite(3, High), digitalWrite(3, low) once, and for each case, I just println(name of the combination)?

Thanks!

It's a lot easier to post code than to read your description.
Please use code tags.

Hi I have downloaded a library to test whether my hardware was connected correctly. But the compiler says there is an error. Will anyone knows why please tell me? Thanks

There's no "library" to test connection. Just write an empty sketch and try to upload it.

But weren't we talking about led blink patterns :astonished:

Yes, my project is about how to change the blinking pattern. I am really a beginner and I am finding codes online, try to understand them and try to do it on my own. so I come across this line,

while (IRpin_PIN & (1 << IRpin))

Anyone knows what this means and can you kindly explain in words? I know this is somewhat similar to digitalRead (IRpin).

const int buttonPin = 2;
const int ledPin = 12;
// values that change
int pushButtonCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

void setup()
{ pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT); 
  Serial.begin(115200);
}

void loop()
{// read whether the button is high or low
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState){
  if (buttonState == HIGH){
    pushButtonCounter++;
  }
}
switch(pushButtonCounter){
case 0: 
digitWrite(ledPin,HIGH);
break;

case 1: 
digitalWrite(ledPin,LOW);
break;
}
}

So this is the draft of the program. The HIGH and LOW at the end will be changed to different patterns later. The problem I have is that when I press the button, it does not go to the next case. Thanks in advance!

Put a System.println(counter) at the end of loop.

You're not resetting counter to 0. You either need a default: counter = 0 inside the switch() statement, or a test like if (counter > 1) counter = 0 after counter++.

Puls, I'd add a delay(100) at the end of loop().

Hi, I must admit that I am a little confused by the above code, but I think that I have an answer to your original question.

So what your asking is, if you have lets say 3 different patterns for your LED, then how do you make each one run without having to write them out every time? If this is what your asking then I believe you could use something called a function. Basically you could write a mini program at the bottom of your main one and then call it in your main program when you need it. For example you function might look like this,

Void pattern1()
{
digitalWrite(3, HIGH);
delay(200);
digitalWrite(3, LOW);
delay(200);
}

Then in your main program whenever you want pattern 1 to run you just type,

pattern1();

And this will find your mini program at the bottom of the code called pattern1 and run it, so then you can write one for pattern2 and call pattern2(); whenever you need it. Now I must admit I am a new to this as well so I might be going about it the long or wrong way but hope it helps!

SamuelCB is right, but the multiple pattern concept is better implemented with the "blink without delay" approach, rather than with a series of delay() inside each pattern function.