Having a bit of a problem with a function that needs to do a little counting.
I've got a photoresistor hooked up to an led and want the led to go on with one handswift and off with 2 handswifts, but somehow I can't wrap my head around it:)
Here's the function code:
void GledOnAndOff(){
int sensorCounter = 0;
//Turn on Gled if sensorValue == 0.
if(sensorValue == 0){
sensorCounter++;
Serial.print("SensorCounter is: ");
Serial.println(sensorCounter);
delay(2);
//Turn on Led with one handswift of the resistor.
if(sensorCounter == 1)
digitalWrite(Gled, HIGH);
//Turn off the led with 2 handswift of the resistor.
if(sensorCounter == 2)
digitalWrite(Gled, LOW);
// Reset sensorCounter > 2
if(sensorCounter > 2){
sensorCounter = 0;
}
}
Where is the rest of your code? What you have provided won't compile.
What exactly is the problem? For example are you able to detect "handswifts" (whatever they are) reliably but can't get the code to cause the right things to happen? OR ... are you having trouble detecting the "handswifts".
Whichever is the case you could probably write a short sketch that just illustrates the problem.
I've tried uploading the ino instead, I couldn't get all the code fitted.
By a handswift i properly mean if the resistor goes from 0 - 255, then everytime it hits 0 = darkness / my hand above it, it makes an increment which I will use to control an led.
I figure the problem is in the function GledOnAndOff(), it's here i'm trying to make the increment.
I am able to get the "0" reading from the resistor, and it will also turn on the Led, however I can't get it to turn off again.
Write out, using a pencil and paper, the states you want and the steps needed to change from each state to the next. That should give you the basics of your program flow.
It sounds like you haven't got the LDR detector working properly yet, so just write a short sketch that reads the LDR value and displays it on the Serial Monitor.
It would be useful if you draw a diagram of how the LDR is wired (I presume you have a voltage divider circuit?) and post a photo of the drawing.
How is the LDR illuminated?
I think you will find that the LDR gives a range of values and that it doesn't respond instantly.