small help needed!

I have this Code, how can I add one more state?

First (not in the code), I want the ledPin to blink every 300ms

then press the button and the led stays HIGH,
then press again and the led goes LOW.


int buttonPin = 7;
int ledPin = 13;
boolean on = false;
int buttonState = 0;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
if(on==true){
on=false;
} else{
on=true;
}
}

if (on ==true) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
delay(200);
}

One more state:

int state = 0;
...
// to cycle through states 0,1,2
state++;
if (state > 2) state = 0;

aarg:
One more state:

int state = 0;

...
// to cycle through states 0,1,2
state++;
if (state > 2) state = 0;

The problem is that I tried that but I can't make it work!

The problem is, we can’t see what you tried

You are really not following the forum guidelines. Of course we can’t guess what you did wrong, we can’t see the code! Without that, all I can suggest is to go and have a beer. Please read the sticky post, “how to post a programming question”, and apply the principles there to all your posts, or this thread will become a long drawn out tragicomedy like this one of yours did:

I am truly sorry but I am new to the Arduino world.

I've tried the code below but...
The problem is that the first state (blink led every 300 ms works but I have to have the button pressed. I need to do the same thing but with one press of the button only.

The rest of the states work fine.

The code:

int buttonPin = 7;
int ledPin = 13;
boolean on = false;
int buttonState = 0;
int ledState = 0;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop(){
buttonState = digitalRead(buttonPin);

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

if (buttonState == HIGH) {
if(on==true){
on=false;
} else{
on=true;
}
}

if (on ==true) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
delay(300);

}

dimitri_ch:
I am truly sorry but I am new to the Arduino world.

You said that in the other thread too, but it has nothing to do with what I'm telling you. All the forum instructions are available in the sticky threads at the top of the forum. They are put there specifically to inform new users like you. Please read them and try to understand. For one thing, you should always put code inside code tags.

I have to have the button pressed

delay(300);
300 milliseconds, you say?

TheMemberFormerlyKnownAsAWOL:

delay(300);

300 milliseconds, you say?

yes! 300 milliseconds!!

Have a look at the state change example in the IDE’s example code.

I need to do the same thing but with one press of the button only.

Look at the StateChangeDetection example in the IDE. You can use the technique to detect the amount of time a button is pressed in your other thread too.

aarg:
You are really not following the forum guidelines. Of course we can't guess what you did wrong, we can't see the code! Without that, all I can suggest is to go and have a beer. Please read the sticky post, "how to post a programming question", and apply the principles there to all your posts, or this thread will become a long drawn out tragicomedy like this one of yours did:
https://forum.arduino.cc/index.php?topic=693945.0

aarg:
You said that in the other thread too, but it has nothing to do with what I'm telling you. All the forum instructions are available in the sticky threads at the top of the forum. They are put there specifically to inform new users like you. Please read them and try to understand. For one thing, you should always put code inside code tags.

Hopefully one day I 'll become wiser with your help!!! Thanks anyways! I already did what you suggested but it seems like I missed some of the threads' suggestions. It's not always so easy to absorb so many different staff in a single day.