Programming help

Hi , before asking my question i notice I’m french , so if you can use easy english language , THX

So , my problem is I do a program for my school and i’m really bad at this.

I have an Arduino MEGA ADK and is connected with USB and i need to do an animation at the led wich is connected to the PIN 9

I give you the program :

int ledPin = 9;      // LED connectée sur la broche 9
int var = 0;          //intensité de la LED (0 à 255)
int presence = 8;    // broche 8
int valeurpre;      //valeur entrée broche (0 ou 5V)

void setup()
{
  pinMode(ledPin, OUTPUT);  // configure la broche en sortie
  pinMode(presence, INPUT);   //configure la broche en entrée
}

void loop()
{
  valeurpre = digitalRead(presence);  //lie l'entrée de le broche pour le capteur présence (0 ou 5V)
  if(valeurpre == 1)
  {
  for(int var = 0; var <= 255; var=var+15)  //effet de progression avec la lumière
    {
     analogWrite(ledPin, var);
   if(var >=255) {                            //
     while(valeurpre == 1) {                  // Maintien l'intensité tant que l'entrée du capteur est égale a 1 (5V)
       valeurpre = digitalRead(presence);     //
       var=255;                               //
                           }
                 }
     
   delay (200);
    }
  }
  
  valeurpre = digitalRead(presence);
  if(valeurpre == 0)
  {
  
for(int val = 255; val = 0; val=val-15)
  {
    
  analogWrite(ledPin, val); 
  if(var <=10) {
     while(valeurpre == 0) {
       valeurpre = digitalRead(presence);
       var=0;
                           } 
                }
  delay (200);
  }
}
}

My program is use for detect presence and turn on the light with a little animation.

But when i launch the program , the model doesn’t detect the 0V with the sensor pin ( 8 ), I think is is stuck in the loop for the action when the sensor detect someone .

Can you help me please ?

Thanks to you :smiley:

Add some prints - find out what your sketch is doing.

As i understand it is something like this you want to accomplish?

dim up led.PNG

Classic C error on this line - can you see it?

for(int val = 255; val = 0; val=val-15)

AWOL:
Add some prints - find out what your sketch is doing.

Wait 5 minutes I do this.

nilton61:
As i understand it is something like this you want to accomplish?

Exactly

MarkT:
Classic C error on this line - can you see it?

for(int val = 255; val = 0; val=val-15)

Sorry but i don’t see the error :confused:

How do you test if a variable is equal to zero ? How do you set a variable to zero ?

This lend itself very well for implemntation by a state machine. I have attached the state diagram below.
The code is based on my state machine library: http://playground.arduino.cc/Code/SMlib
instructions for installing libraries are here: http://arduino.cc/en/Guide/Libraries

#include <SM.h>

SM presDim(Wait);

const int ledPin = 9;      // LED connectée sur la broche 9
const int presence = 8;    // broche 8
int dimValue;

void setup(){
  pinMode(ledPin, OUTPUT);
}//setup()

void loop(){
  EXEC(presDim);
}//loop()

State Wait(){
  if(digitalRead(presence)) presDim.Set(dimUpH, dimUpB);
}//Wait()

State dimUpH(){
  dimValue++;
  analogWrite(ledPin, dimValue);
}//dimUpH()

State dimUpB(){
  if(presDim.Timeout(8)) presDim.Restart();
  if(!digitalRead(presence)) presDim.Set(dimDownH, dimDownB);
  if(dimValue >= 255) presDim.Set(hold);
}//dimUpB()

State hold(){
  if(!digitalRead(presence)) presDim.Set(dimDownH, dimDownB);
}//hold()

State dimDownH(){
  --dimValue;
  analogWrite(ledPin, dimValue);
}//dimDownH()

State dimDownB(){
  if(presDim.Timeout(8)) presDim.Restart();
  if(digitalRead(presence)) presDim.Set(dimUpH, dimUpB);
  if(dimValue <= 0) presDim.Set(Wait);
}//dimDownB()

presence dimmer state diagram.png

AWOL: Add some prints - find out what your sketch is doing.

Here is the pict

http://www.hostingpics.net/viewer.php?id=38286220131204101227final.jpg

UKHeliBob: How do you test if a variable is equal to zero ? How do you set a variable to zero ?

I do it with an resistor in the pin 8 to the GND.

And i don't know how to test teh 0 beacause i don't find the debug consol.

nilton61: This lend itself very well for implemntation by a state machine. I have attached the state diagram below. The code is based on my state machine library: http://playground.arduino.cc/Code/SMlib instructions for installing libraries are here: http://arduino.cc/en/Guide/Libraries

Your program work :) but before the animation when the led turn on smoothly , at the beginning it is a little "flash" and after the program work perfectly.

I don't know why.

I dont have that behavior when i run the code. So sorry, cant help

But i don't really understand your program, what is : "State" "presDim.Set(dimDownH, dimDownB)" And why the "!" in the "if(!digitalRead(presence))"

UKHeliBob: How do you test if a variable is equal to zero ? How do you set a variable to zero ?

I do it with an resistor in the pin 8 to the GND.

And i don't know how to test teh 0 beacause i don't find the debug consol.

I will put it another way. Is there a difference betweenval = 0;andval == 0;

State is the (return) type of a state function. Actually its just a tydef'd void but this aids to the readability of the code that is actually a state function. presDim is the name of the state machine. It is defined at the beginning of the program together with it initial state.

SM presDim(Wait)//create a statemachine object called presDim and set ints initial state to State Wait()

this is a little more compliacted:

presDim.Set(dimDownH, dimDownB)

With the library you have the opportunity to use head- and body states. That is On the execution of EXEC(presDim) (in loop) after the Set() function is called dimDownH (the head function) will be called. Here dimValue is changed and the value is written to the pin. We need to do this only once every 8 ms. Using a head function ensures this. next time EXEC(presDim) is executed dimDownB is called. Here we wait for 8 ms before we start over with dimDownB. During the wait we check if presence is high. in that case we divert to the dimUp states (head and body here also). If dimValue is 0 we revert to Wait()

! means not in C that is:

if(!digitalRead(presence))

checks if presence is NOT pressed.

Sorry but it's really complicate , I need to explain my final program for have my examen in school. So I need a simple program with a basic language and do not use library because i don't know how to make it. If you can do a simple program like my first, I really appreciate it.

Thanks again for your help :)

Have you spotted the problem with the "for" loop yet? If not, study reply #3 again.

Or you do some reading on state machines.. THAT will give some good grades

MarkT: Classic C error on this line - can you see it?

for(int val = 255; val = 0; val=val-15)

I don't see the error ..

AWOL: Have you spotted the problem with the "for" loop yet? If not, study reply #3 again.

can you help me ?

EDIT: I see the error

MarkT: for(int val = 255; val = 0; val=val-15) for(int var = 255; var = 0; var=var-15)

What is the difference between

val = 0

and

val == 0

Once you can tell that difference you should be able to spot the error

So, now this is my program:

void loop()
{
  valeurpre = digitalRead(presence);
  if(valeurpre == 1)
  {
  for(int var = 0; var < 255; var=var+16)
    {
     analogWrite(ledPin, var);
   if(var >=255) {
     while(valeurpre == 1) {
       valeurpre = digitalRead(presence);
       var=255;
                           }
                 }
     
   delay (200);
    }
  }
  
  valeurpre = digitalRead(presence);
  if(valeurpre == 0)
  {
  
for(int var =256; val = 0; var=var-15)
  {
    
  analogWrite(ledPin, val); 
  if(var <=10) {
     while(valeurpre == 0) {
       valeurpre = digitalRead(presence);
       var=0;
                           } 
                }
  delay (200);
  }
}
}

But now when presence = 1 the led do the first animation ( 0 → 255) and again , again, agin …
And when presence = 0 the led is continually ON …

I don’t understand why --’

Still the same error we are trying to point out to you......