Go Down

Topic: motor runing one lap (Read 494 times) previous topic - next topic

THX_RoG

im trying to make my motor to run  only one lap wait and to run another lap but have some issue
Code: [Select]
int motor=2;
int button=3;
void setup(){
  pinMode(motor, OUTPUT);
  pinMode(button, INPUT);
  Serial.begin(115200);
  Serial.print("REDY");
}
void loop(){
  while(Serial.available()>0)
  {
    char aChar=Serial.read();
    if (aChar=='g')
    {
      Serial.println("RUN");
      digitalWrite(motor,HIGH);
      digitalRead(button)==LOW;
    }
  }
  if (digitalRead(button)==HIGH)
  {
    Serial.println("STOP");
    digitalWrite(motor, LOW);
  }
}

i try different setups wit this code but my motor run one lap or he stops and stays stop or hes not runing
UNO,MEGA2560,MEGA2560
Killed:UNO, MEGA2560, ... More to come

dc42

The following line:

Code: [Select]
digitalRead(button)==LOW;

does nothing useful (it reads the pin, compares the value with LOW, and throws the result away).

How do you have the button connected to the Arduino?
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

THX_RoG

like i write i try difrent setups im using PIN 3 to button and PIN 2 for motor
hers some photos
http://imageshack.us/a/img11/5/20121028195540.jpg
http://imageshack.us/a/img713/1323/20121028195220.jpg
UNO,MEGA2560,MEGA2560
Killed:UNO, MEGA2560, ... More to come

dc42

1. Unless you have an external pullup resistor connected, you need to change pinMode(button, INPUT) to pinMode(button, INPUT_PULLUP).

2. Is the switch closed at the "home" position, or open?

3. After you start the motor, you will need to wait for the motor to move away from the home position before you start looking to see whether the switch indicates that the home position has been reached again.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

THX_RoG

#4
Oct 29, 2012, 05:49 pm Last Edit: Oct 29, 2012, 06:50 pm by THX_RoG Reason: 1
the home position is open
if the motor run the switch is closed
have modified the code
Code: [Select]
int motor=2;
int sw=3;
void setup(){
  pinMode(motor, OUTPUT);
  pinMode(sw, INPUT_PULLUP);
  Serial.begin(115200);
  Serial.print("REDY");
}
void loop(){
  while(Serial.available()>0)
  {
    char aChar=Serial.read();
    if (aChar=='g')
    {
      Serial.println("RUN");
      digitalWrite(motor,HIGH);
    }
  }
  if (digitalRead(sw)==HIGH)
  {
    Serial.println("STOP");
    digitalWrite(motor, LOW);
  }
}

but now my serial print STOP all the time and i cant restart the motor
UNO,MEGA2560,MEGA2560
Killed:UNO, MEGA2560, ... More to come

dc42

#5
Oct 30, 2012, 09:07 am Last Edit: Oct 30, 2012, 09:08 am by dc42 Reason: 1
After the line

Code: [Select]
digitalWrite(motor,HIGH);

to start the motor, you need to keep the motor on until the switch closes, before you proceed to wait for the switch to open again. Try inserting this after that line:

Code: [Select]
while (digitalRead(sw) == HIGH) { }
delay(10);


The first line waits for the switch to close and the second one allows for contact bounce. Alternatively, just use delay(1000) so that the motor runs for at least one second, which is hopefully long enough for the switch to close.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

THX_RoG

UNO,MEGA2560,MEGA2560
Killed:UNO, MEGA2560, ... More to come

Go Up