unsigned long count = 0;
void setup() {
pinMode (10,INPUT);
pinMode (6,OUTPUT);
Serial.begin (9600);
}
void loop() {
if (digitalRead(10)==HIGH) {digitalWrite (6,HIGH);}
else {digitalWrite (6,LOW); Serial.println (count);}
while (digitalRead(10)==HIGH) {count ++;}
}
[code]
Please excuse my ignorance or etiquette as this is the first post in my life.
In the above sketch pin 10 is connected to a switch, and pin 6 is connected to an led.
Sketch was to count number of "scans" or "loops" while input high and then display count to the serial monitor when input went low. output to led was just for visual that input was high.
Counting and displaying count to serial monitor all worked great.
However...output to led (pin 6) does not come on when the input is high.
Input and output work fine when commenting out the wait function.
My assumption was that with the "if" function ahead of the "while" function, it would digital write pin 6 high, and then go to the "wait" function.
Obviously I am not understanding when functions are actually executed.
Can anyone please help me understand "when" digital writes to an output are "actually" executed?
I am now wondering if "outputs" are actually updated at the end of a loop rather than when and where they are in the sketch.
Any explanation someone can help me to understand will be greatly appreciated.
Thank You
You forgot the slash in the closing code tag. We do appreciate that you tried to use code tags.
Your formatting is not great. You should put every brace and statement on a separate line and use auto format (ctrl-T) for correct indentation:
void loop()
{
if (digitalRead(10) == HIGH)
{
digitalWrite (6, HIGH);
}
else
{
digitalWrite (6, LOW);
Serial.println (count);
}
while (digitalRead(10) == HIGH)
{
count ++;
}
}
There is likely nothing wrong with your program logic, concerning the output pin. More likely you have a hardware issue. Please load the blink sketch and change the pin number to 6, to test it.
Thanks aarg for your suggestions, both in posting and what to try.
Loaded "blink" and used pin 6 as output...No problem...digital pin 6 works fine.
As mentioned in original post, when I comment out ( // ) the wait function the led @ pin 6 does come on and off with the switch @ pin 10.
@OP. Yes, the button has to be active at the moment the function is called, for it to work. I was forced to assume that is what you wanted, since you didn't post any other code. If not, then you have to reconsider your logic. Did you want to wait for a button press, inside the function? That's different. You didn't ask for that.
while (digitalRead(10)==HIGH) {count ++;}
Imagine this conversation happening all in the space of a tenth of a second or so:
"Is pin 10 high?"
"Yes."
"Ok. Add 1 to count."
"Is pin 10 high?"
"Yes."
"Ok. Add 1 to count."
"Is pin 10 high?"
"Yes."
"Ok. Add 1 to count."… times several thousands …
"Is pin 10 high?"
"Yes."
"Ok. Add 1 to count."
"Is pin 10 high?"
"No."
"Ok. All done!"
Count will wind up being set to some more-or-less random value.
Based on thought that timing of "exactly" when switch (input) comes on being so critical relative to where the loop is in it's scan revised sketch to below. Also wanted to follow basic "KISS" rule. Now works like a champ.
Thanks to all who contributed. Esp. Delta_G for the "getting hung" thought.
unsigned long count = 0;
void setup() {
pinMode (10,INPUT);
pinMode (6,OUTPUT);
Serial.begin (9600);
}
void loop() {
while (digitalRead(10)==HIGH) {count ++; digitalWrite (6,HIGH);}
if (digitalRead(10)==LOW) {digitalWrite (6,LOW);}
if (digitalRead(10)==LOW && count >1) {Serial.println (count);}
count = 0;
}
I apologize if I haven't gotten the "code" thing quite right.
Personally, I'd keep a separate boolean value to hold the 'previous' state of the input, and compare that to the current state...
If they are different - perform your logic and LED / counts / reset etc.
If they are the same - you can keep track of the up count or down count as you like.
Keep in mind - the count value is going to run up VERY quickly - and delay() is not the right solution.
Look into keeping track of millis() between samples - and test the button every (however long - to debounce), then test the button state as often as you want the counter to increment.