Hy evry one i need your help for a class room project

//Je suis français / I am french\ :slight_smile:

I need to make a programe who make this objective :

1Bp
1Led
1Arduino card

/Start program\

-Led OFF
-Press the button → led ON (do not flash)
-Repress the button → led must be flash (delay 100)
-repress the buton → Go to the top of the program where led is OFF

I block orround 3hours on the grogram i make, than’s for your helps. :sweat_smile:
Than’x so mutch.

T’is is my programm i don’t now where i can up the value of “i” when i press the button and change the “state” :confused:

///////////////////////////////////////////////////////////////
const int led = 2;
const int BP = 12;
int i;

void setup()
{
pinMode(led, OUTPUT);
pinMode(BP, INPUT);

}

void loop()
{
for(i=0; i<3;1++ )

{
x=x+1;
{
ETAT_BP=digitalRead(BP);
if (ETAT_BP==APPUI){

digitalWrite(LED,1);

}

else {

digitalWrite(LED,0);
}

}
}
}

if ETAT_BP=HIGH;
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

XOXOXOXOXOXOXOXOXOXO

? Maybe you should try the French forum. But read the "How to use this forum" before!

You need to think in a more structured way.

Your system needs 3 modes. (off, on, flashing)

Define a variable to remember which mode you're in.

Write code that increments this variable when you press the button and goes back to the first mode when necessary.

Use Serial.print statements to verify it's working.

Then write the code for each mode, that makes the LED do what you need.

I don't want to tell you too much, for the good of your education!

Good luck! :)

for(i=0; i<3;1++ )

What good does it do to increment the value of 1? 1++ returns 2. But, so what? You are NOT incrementing the value of i.

Bearonic:
?
Maybe you should try the French forum. But read the “How to use this forum” before!

This forum is monsioned “french arduino forum” i will post it her

GypsumFantastic:
You need to think in a more structured way.

Your system needs 3 modes. (off, on, flashing)

Define a variable to remember which mode you’re in.

Write code that increments this variable when you press the button and goes back to the first mode when necessary.

Use Serial.print statements to verify it’s working.

Then write the code for each mode, that makes the LED do what you need.

I don’t want to tell you too much, for the good of your education!

Good luck!
:slight_smile:

that is what i try while 3hours… or teatcher help other peapoles but not me… he say"go to internet…" but code the programe to other peapoles…
he juste tel me “yep you can use for loop” and my partner does anything…

PaulS:

for(i=0; i<3;1++ )

What good does it do to increment the value of 1? 1++ returns 2. But, so what? You are NOT incrementing the value of i.

I… Hum just want to increment the I value for switch mode led ON, Flash,Off

Look closely at your for loop. You have a 1 where you meant i

Let's work it out ,

You create three states , it stores what the program has do to next.

I am just providing the structure , you can workout the rest . (If you can't , ask again...)

int state=0;
const int led = 2;
const int BP = 12;

void setup()
{
pinMode(led, OUTPUT); 
pinMode(BP, INPUT);

  

}

void loop{

   if(state=0){

//put your code here
   state=1;

 }
   if(state=1){

//put your code here
   state=2;

 }
 if(state=2){

//put your code here
   state=0;

 }


  

}

Edryu:
I am just providing the structure , you can workout the rest .

   if(state=0){

if(state=1){
if(state=2){

Ups.3

Yes, need ==s there. Or this:

switch (state) {
case 0:
//code
break;
case 1:
//code
break;
case 2:
//code
break;
}

Then you're not going thru 3 comparisons very time, just jumping right to the correct one.

In my last project I did something like this.

while (state == 0){
//do whatever it takes to make LED off
//check for pressed button, increment state etc
}

while (state ==1){
//do whatever it takes to make LED on
//check for pressed button, increment state etc
} 

while (state ==2){
//do whatever it takes to make LED flash
//check for pressed button, put state back to zero etc
}

But that's mainly because the separate states in my project needed to be loops. I think the flashing state probably would here. All the different ways have their advantages I think.

#include <Bounce2.h> //https://github.com/thomasfredericks/Bounce2

const byte buttonPin = 2;
const byte ledPin = 12;
const unsigned long onTime = 100;

enum LedState {
  ledOff,
  ledOn,
  ledBlink,
};

Bounce button;
byte ledState = ledOff;
unsigned long lastLedMillis;

void setup() {
  pinMode(ledPin, OUTPUT);
  button.attach(buttonPin, INPUT_PULLUP);
}

void loop () {
  if (button.update()) { // button activity?
    if (button.fell()) { // press event?
      if (++ledState > ledBlink) { // next state, wrap
        ledState = ledOff;
      }
      switch (ledState) {
        case ledOff:
          digitalWrite(ledPin, LOW);
          break;
        case ledBlink:
          lastLedMillis = millis();
          //fall through
        case ledOn:
          digitalWrite(ledPin, HIGH);
          break;
      }
    }
  }
  if (ledState == ledBlink && (millis() - lastLedMillis > onTime)) {
    lastLedMillis = millis();
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
}

In my last project I did something like this.

Ugh. Nasty, blocking code. Why do you say that the states in your code needed to be loops ? What do you think the loop() function is for ?

It's a school project - he can do state machines some other day.

Using this forum instead of classroom discussions will make you street smart ( "typos are bad") , not computer savvy. C'est la vie

Whandall:

#include <Bounce2.h> //https://github.com/thomasfredericks/Bounce2

const byte buttonPin = 2;
const byte ledPin = 12;
const unsigned long onTime = 100;

enum LedState {
  ledOff,
  ledOn,
  ledBlink,
};

Bounce button;
byte ledState = ledOff;
unsigned long lastLedMillis;

void setup() {
  pinMode(ledPin, OUTPUT);
  button.attach(buttonPin, INPUT_PULLUP);
}

void loop () {
  if (button.update()) { // button activity?
    if (button.fell()) { // press event?
      if (++ledState > ledBlink) { // next state, wrap
        ledState = ledOff;
      }
      switch (ledState) {
        case ledOff:
          digitalWrite(ledPin, LOW);
          break;
        case ledBlink:
          lastLedMillis = millis();
          //fall through
        case ledOn:
          digitalWrite(ledPin, HIGH);
          break;
      }
    }
  }
  if (ledState == ledBlink && (millis() - lastLedMillis > onTime)) {
    lastLedMillis = millis();
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
}

WooooooWWWW than’s a looot !! But how i can include <Bounce2.h> ? i go on the website and i try tu copy/paste the code in the program but don’t works :confused: i just need to download a file ? how i intergrated it ?

I made this montage but is not the same what i make in Protheus (in proteus the montage work’s fine)

Thanks so mutch for all the answer _

I find it !! I added "Bounce2" the libreries on arduino program The last think to do is the electonic assembly correctly :D I will come back tomorrow Have a nice day THANKXXX