I'm trying to learn programming and Electronics together and was wondering the following -
Is it possible to use a Do While Structure to turn an LED on or Off with a Capacitive Sensor?
If so, what might be some example code?
I'm trying to learn programming and Electronics together and was wondering the following -
Is it possible to use a Do While Structure to turn an LED on or Off with a Capacitive Sensor?
If so, what might be some example code?
Is it possible to use a Do While Structure to turn an LED on or Off with a Capacitive Sensor?
IMO, it would not be particularly applicable. First express what you want to do in english. Something like:
if the capacitance sensor ON button is touched, turn the LED off.
if the OFF button is touched, turn the LED off.
See? No "while" loop. Oh, I guess there's an implicit "repeat this forever" around the whole thing, but that's not a particularly good reason for picking a do..while construct over some other form of infinite loop.
I'm just trying to be as creative and imaginative as I can with learning how the programming side of things work.
What I'd like to do is turn an Led light on with a touch sensor. I'd like to see how many ways this can be accomplished. I don't want to over complicate something fairly simple, just experiment with the possibilities.
To that end, I'd like to try this currently with a Do While loop.
Try this
void loop()
{
do
{
digitalWrite(ledPin, HIGH);
}
while (digitalRead(buttonPin) == LOW);
digitalWrite(ledPin, LOW);
}
It works, sort of, but the LED will light dimly all the time and brightly when the button is pressed. It glows dimly because the instructions in the do/while are always executed before the while test is done, unlike while/do, so the LED is turned on each time through loop() then off again immediately. On the whole, do/while is more useful in my experience.
You will need to modify this code to work sensor if it returns an analogue value.
UKHeliBob -
Thanks for that. I hadn't thought about that particular approach, but it's helpful.
What approach had you got in mind ? It may still be a valid way to do it.
Well, without posting the code here, I'll just sat that it seemed I could only ever get an LED to stay on constantly or off constantly as the Do While loop seemed to quit after any condition I could think of.
The only possible way I think this may have worked would be to end the code with something like while (Variable == AnyRealNumber);
I suppose that AnyRealNumber could be swapped with any arbitrary condition that is always likely to be met.
That said, for turning the LED on when the touch sensor is well, touched, it's probably best to stay with an If Else such that there is no definite end condition that prevents the LED from ever lighting up.
Thanks all for your input here.
Sorry. You have lost me but I am glad to have been of help.
Post your code if you want feedback on it.