Offline
Newbie
Karma: 0
Posts: 11
|
 |
« on: April 28, 2012, 05:54:10 am » |
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!  #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
Tesla Member
Karma: 108
Posts: 6607
|
 |
« Reply #1 on: April 28, 2012, 07:05:41 am » |
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
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #2 on: April 28, 2012, 11:11:39 am » |
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
Brattain Member
Karma: 299
Posts: 26024
Solder is electric glue
|
 |
« Reply #3 on: April 28, 2012, 11:19:50 am » |
At the end of the loop() have the following line:- oldstate = state; Define oldstate just like you defined state in the same place.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #4 on: April 28, 2012, 12:51:54 pm » |
thx Grumpy. So now i've this and it gives me problem like before, maybe better but not so much. #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
Brattain Member
Karma: 299
Posts: 26024
Solder is electric glue
|
 |
« Reply #5 on: April 28, 2012, 01:26:50 pm » |
This is because you are not using the value of oldstate. Change:- if (state == 0) { to if (state == 0 && oldstate == 1) {
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #6 on: April 28, 2012, 01:39:44 pm » |
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. #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
Online
Edison Member
Karma: 51
Posts: 2175
|
 |
« Reply #7 on: April 28, 2012, 01:54:51 pm » |
It doesn't function and the led is ever on. Of course it is. 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
Brattain Member
Karma: 299
Posts: 26024
Solder is electric glue
|
 |
« Reply #8 on: April 28, 2012, 01:59:54 pm » |
I am not sure what you want to do with LED1 but this should toggle LED2 :- #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
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #9 on: April 28, 2012, 05:13:26 pm » |
I am not sure what you want to do with LED1 but this should toggle LED2 :- #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. #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. 
|
|
|
|
« Last Edit: April 28, 2012, 05:26:13 pm by jhacked »
|
Logged
|
|
|
|
|
California
Online
Edison Member
Karma: 51
Posts: 2175
|
 |
« Reply #10 on: April 28, 2012, 05:41:07 pm » |
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
Brattain Member
Karma: 299
Posts: 26024
Solder is electric glue
|
 |
« Reply #11 on: April 28, 2012, 10:06:22 pm » |
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. 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
|
|
|
|
|
|