Pages: [1]   Go Down
Author Topic: Simple question about turning on a led.  (Read 998 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys! How are you? I've a problem and i'm gonna to explain it. I have to turn on a led when i put my finger on the cny70. I know how i can do this, but i've to save the state of the led. I mean that the led, when i raise my finger from the sensor, has to stay on but, putting the finger again on the cny70, the led has to turn off. I wrote this code but it doesn't function very well, i would be very happy if someone of you could give me an hand! smiley-grin

Code:
#define led1 13
#define led2 12
#define sensor A0

int state = 0; 

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(sensor, INPUT);
}

void loop() {
  digitalWrite(led1, HIGH);
  int readsensor = analogRead(sensor);
 
  if (state == 0) { 
    digitalWrite(led2, LOW); 
      if (readsensor < 950) {
        state = 1;
      }
  } else { 
        digitalWrite(led2, HIGH); 
      if (readsensor < 950) {
        state = 1 - state;
      }
  } 
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem is that you aren't keeping track of the previous position of your finger.  The result is that the LED will rapidly alternate between ON and OFF as long as the finger is in position.   You want to change the LED state only when the finger state changes from AWAY to CLOSE.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, i need right that. But i don't know how i can do it. I understand that i should create a variable called "oldstate", but what kind of value i've to give to it? Can you show me a code example?

thank you very much, my friend!
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34725
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

At the end of the loop() have the following line:-
Code:
oldstate = state;
Define oldstate just like you defined state in the same place.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thx Grumpy. So now i've this and it gives me problem like before, maybe better but not so much.

Code:

#define led1 13
#define led2 7
#define sensor A0

int state = 0; 
int oldstate = 0;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(sensor, INPUT);
}

void loop() {
  digitalWrite(led1, HIGH);
  int readsensor = analogRead(sensor);
 
  if (state == 0) { 
    digitalWrite(led2, LOW); 
      if (readsensor > 15) {
        state = 1;
      }
  } else { 
        digitalWrite(led2, HIGH); 
      if (readsensor > 15) {
        state = 1 - state;
      }
  } 
  oldstate = state;
}

Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34725
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This is because you are not using the value of oldstate.

Change:-
Code:
if (state == 0) { 
to
Code:
if (state == 0 && oldstate == 1) { 
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, i understood that add only a simple assignament function shouldn't change a damn, but i didn't know what i had to do with the value of "oldstate".

It doesn't function and the led is ever on.

Code:
#define led1 13
#define led2 7
#define sensor A0

int state = 0; 
int oldstate = 0;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(sensor, INPUT);
}

void loop() {
  digitalWrite(led1, HIGH);
  int readsensor = analogRead(sensor);
 
  if (state == 0 && oldstate == 1) { 
    digitalWrite(led2, LOW); 
      if (readsensor > 15) {
        state = 1;
      }
  } else { 
        digitalWrite(led2, HIGH); 
      if (readsensor > 15) {
        state = 1 - state;
      }
  } 
  oldstate = state;
}
Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It doesn't function and the led is ever on.
Of course it is.

Code:
void loop() {
  digitalWrite(led1, HIGH);
  int readsensor = analogRead(sensor);
  
  if (state == 0 && oldstate == 1) {
  ...
  oldstate = state;
}
Think about this for a second. At the end of every loop, you're assigning oldstate to state. When the loop starts again, no change is made to state, so when execution goes to the if statement, they'll never be different, thus, your if statement will always fail.
« Last Edit: April 28, 2012, 02:02:30 pm by Arrch » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34725
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am not sure what you want to do with LED1 but this should toggle LED2 :-

Code:
#define led1 13
#define led2 7
#define sensor A0

boolean state = true; 

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(sensor, INPUT);
}

void loop() {
  if(state){
      while(analogRead(sensor) > 15) {
      digitalWrite(led2, HIGH);
      state = true;
  }
  else {
      while(analogRead(sensor) > 15) {
      digitalWrite(led2, LOW);
      state = false;
  }
}

This assumes when your finger is on the sensor you get a value greater than 15 and lower than 15 when it is removed.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am not sure what you want to do with LED1 but this should toggle LED2 :-

Code:
#define led1 13
#define led2 7
#define sensor A0

boolean state = true;  

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(sensor, INPUT);
}

void loop() {
  if(state){
      while(analogRead(sensor) > 15) {
      digitalWrite(led2, HIGH);
      state = true;
  }
  else {
      while(analogRead(sensor) > 15) {
      digitalWrite(led2, LOW);
      state = false;
  }
}

This assumes when your finger is on the sensor you get a value greater than 15 and lower than 15 when it is removed.

the led1 constant has to stay always on, because it's the cny70 sensor led. The code that you gave me is the same of the first one that I posted here.

Code:
#define LED 13                // LED collegato al pin digitale 13  
#define BUTTON 7              // pin di input dove è collegato il pulsante  
int val = 0;                  // si userà val per conservare lo stato del pin di input  
int vecchio_val = 0;          // si userà vecchio_val per conservare lo stato del pin di input al passo precedente  
int stato = 0;                // ricorda lo stato in cui si trova il led, stato = 0 led spento, stato = 1 led acceso  
  
void setup() {  
  pinMode(LED, OUTPUT);       // imposta il pin digitale come output  
  pinMode(BUTTON, INPUT);     // imposta il pin digitale come input  
}  
  
void loop() {  
  val = digitalRead(BUTTON);  // legge il valore dell'input e lo conserva  
  
  // controlla se è accaduto qualcosa  
  if ((val == HIGH) && (vecchio_val == LOW)){  
    stato = 1 - stato;  
    delay(15);                // attesa di 15 millisecondi  
  }  
  
  vecchio_val = val;            // ricordiamo il valore precedente di val  
  
    if (stato == 1) {  
    digitalWrite(LED, HIGH);   // accende il led  
  }  
  else {  
    digitalWrite(LED, LOW);    //spegne il led  
  }  
}

I have to do exactly the same thing but with the cny70 sensor and its values. Do you understand now?

Thank you very much to all of you, i don't know what I could do without you. smiley-grin
« Last Edit: April 28, 2012, 05:26:13 pm by jhacked » Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So take that code and modify how the data is determined (digitalRead) the if statement conditions to match the device you are using.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34725
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The code that you gave me is the same of the first one that I posted here.
I don't understand that. Clearly it is very different code so what do you mean?

I am thinking that the title of this thread is rather misleading, clearly there is nothing "simple" about what you are trying to do.

Quote
I have to do exactly the same thing but with the cny70 sensor and its values. Do you understand now?
Sorry but no. Can you provide a link to the sensor and state again exactly what you want to do.
« Last Edit: April 28, 2012, 10:08:31 pm by Grumpy_Mike » Logged

Pages: [1]   Go Up
Jump to: