Hello and thank you for reading this!
I have a program which drives a motor 2 ways with and IC and I had no problems with it up until I needed to add 2 switches to control its movement. This is the original program (segment of it):
void setup()
{
digitalWrite(motorpin1,LOW); //direction up
digitalWrite(motorpin2,HIGH);
digitalWrite(pmwpin,HIGH); //start
delay(5000); //move for 5 seconds
digitalWrite(pmwpin,LOW); //stop
delay (50000); //wait 50 seconds
digitalWrite(motorpin1,HIGH); //change the motor direction
digitalWrite(motorpin2,LOW);
digitalWrite(pmwpin,HIGH); //start
delay(140); //goes for 140 ms
digitalWrite(pmwpin,LOW); //it stops, end of the code
}
As you can see from my program it used to run in the setup segment but because from now I need to check always the state of the button it was moved there (switch1 and switch2; true means it's pushed
void loop()
{
digitalWrite(motorpin1,LOW); //direction up
digitalWrite(motorpin2,HIGH);
digitalWrite(pmwpin,HIGH); //start
while ( switch1 == true) {
digitalWrite(pwmpin, HIGH); //it's turned on until the switch one's state equals to true
}
digitalWrite(pmwpin,LOW); //stop
delay (50000); //wait 50 seconds
digitalWrite(motorpin1,HIGH); //change the motor direction
digitalWrite(motorpin2,LOW);
digitalWrite(pmwpin,HIGH); //start
while (switch2 == true) {
digitalWrite(pwmpin, HIGH); //it's turned on until the switch two's state equals to true
}
digitalWrite(pmwpin,LOW); //it stops, end of the code
}
as far as I know, according to the flowchart and the documentation it supposed to do what I've commented there ( move up until s1 pushed, changes direction, waits, then moves down until s2 is pushed then ends the program) but it moves pretty random instead.
I feel like I don't 100% got the concept of looping but I really tried to understand it. If someone could help me I'd appreciate it! Thank you!
