i am trying to make while function for my school project the first on was rejected beacuse i dint use while function . i need to create 3 pushed led goes on and 3 pushes again the led goes off
int buttonPushCounter = 0;
void setup()
{
pinMode(8, INPUT_PULLUP);
pinMode(4, OUTPUT);
}
void loop() {
buttonPushCounter++;
delay(100);
buttonPushCounter = digitalRead(8);
{
while (buttonPushCounter == 3) {
digitalWrite(4, HIGH);
{
{
while(buttonPushCounter == 6)
digitalWrite(4, LOW);
}
buttonPushCounter = 0;
}
}
}
}
buttonPushCounter++;
....
buttonPushCounter = digitalRead(8);
{
Oops
dont understand what you mean did i forgot { } or have to change ++?
int buttonPushCounter = 0;
void setup()
{
pinMode(8, INPUT_PULLUP);
pinMode(4, OUTPUT);
}
void loop() {
buttonPushCounter++;
delay(100);
{
buttonPushCounter = digitalRead(8);{
while (buttonPushCounter == 3) {
digitalWrite(4, HIGH);
}
while(buttonPushCounter == 6)
digitalWrite(4, LOW);
{
buttonPushCounter = 0;
}
}
}
}
gcjr
March 11, 2021, 7:42pm
5
why do you increment buttonPushCounter and then read its' value from pin 8?
why is there a brace, "{", after digitalRead()?
it is very unconventional to have while (buttonPushCounter == 3) which ends because buttonPushCounter is set to zero within the loop. why not use an if?
why is there a brace after digitalWrite (4, HIGH)?
but within the while (buttonPushCounter == 3) loop is a while (buttonPushCounter == 6) loop. what causes the while (buttonPushCounter == 6) loop to exit, what changes the value of buttonPushCounter?
here's how I would indent your code
int buttonPushCounter = 0;
void setup()
{
pinMode(8, INPUT_PULLUP);
pinMode(4, OUTPUT);
}
void loop() {
buttonPushCounter++;
delay(100);
buttonPushCounter = digitalRead(8);
{
while (buttonPushCounter == 3) {
digitalWrite(4, HIGH);
{
{
while(buttonPushCounter == 6)
digitalWrite(4, LOW);
}
buttonPushCounter = 0;
}
}
}
}
gcjr
March 11, 2021, 7:45pm
6
in your 2nd version of code, how does the value of buttonPushCounter change so that the while loops end?
formatted more conventionally
int buttonPushCounter = 0;
void setup()
{
pinMode(8, INPUT_PULLUP);
pinMode(4, OUTPUT);
}
void loop() {
buttonPushCounter++;
delay(100);
{
buttonPushCounter = digitalRead(8);
{
while (buttonPushCounter == 3) {
digitalWrite(4, HIGH);
}
while(buttonPushCounter == 6)
digitalWrite(4, LOW);
{
buttonPushCounter = 0;
}
}
}
}
thnx for the advies i need to make a function in a while 3 button pushes les goes on and when 3 times again it goes off i cant find nowhere how to change buttonPushCounter = digitalRead(8); something else
So you understand what a while loop does?
How to enter the body of the loop, and how you exit it?
LarryD
March 11, 2021, 8:09pm
9
Tell us what this line of code does:
buttonPushCounter = digitalRead(8 );
buttonPushCounter = digitalRead(8 ); Its reads thbepushes of button to active the while
gcjr
March 11, 2021, 8:38pm
11
how does it become a value other than 0 or 1?
Not i understand now , how do you create then a counter or a variable that wil save the pushes
LarryD
March 11, 2021, 9:05pm
13
When your switch ‘goes from HIGH to LOW’ you add one to your counter.
Alright its like if digitalread (8)is high && counter ==1
LarryD
March 11, 2021, 9:53pm
15
No.
‘When’ and only ‘when’ the state of pin 8 goes from HIGH to LOW then you do counter = counter + 1
opelvectra:
Alright its like if digitalread (8)is high && counter ==1
No, you have a pull up on pin 8, so when the switch is open, it will read HIGH.
So, most of the time.
What you need to do is increment the counter when the state of the pin changes from HIGH to LOW.
nt buttonPushCounter = 0;
void setup()
{
pinMode(8, INPUT_PULLUP);
pinMode(4, OUTPUT);
}
void loop() {
delay(100);
digitalRead(8) == buttonPushCounter ;
{
while ((digitalRead(8) == 3) && (buttonPushCounter + 1)); {
digitalWrite(4, HIGH);
{
{
while ((digitalRead(8) == 6) && (buttonPushCounter + 1));
digitalWrite(4, LOW);
}
} if (digitalRead (8) == 6);
buttonPushCounter = 0; {
else }
}
}
}
i tried to write it but got a error somehow and my led its blinking very fast
There is example code in the IDE for reading switches.
Spend some time studying them.
Your code is so way off at the moment, you need to slow down and work at the basics.
but got a error somehow
But somehow, you forgot to post it.
Not helpful.
Programming is not a process of throwing alphabetti spaghetti at a wall, and hoping a program will appear.
LarryD
March 11, 2021, 10:02pm
19
If last state read on pin 8 was HIGH and the state on pin 8 is now LOW then you will increment the value of your counter.
If last state read on pin 8 was HIGH and the state on pin 8 is still HIGH you don’t do anything.
If last state read on pin 8 was LOW and the state on pin 8 is now HIGH you don’t do anything.
my bad it didnt post i see the errror is gonne but stil my led is blinking fast
int buttonPushCounter = 0;
void setup()
{
pinMode(8, INPUT_PULLUP);
pinMode(4, OUTPUT);
}
void loop() {
delay(100);
digitalRead(8) == buttonPushCounter ;
{
while ((digitalRead(8) == 3) && (buttonPushCounter + 1)); {
digitalWrite(4, HIGH);
{
{
while ((digitalRead(8) == 6) && (buttonPushCounter + 1));
digitalWrite(4, LOW);
}
(digitalRead (8) == 6);{
}
buttonPushCounter = 0;
}
}
}
}