Pages: [1]   Go Down
Author Topic: motor runing one lap  (Read 451 times)
0 Members and 1 Guest are viewing this topic.
Germany
Offline Offline
Jr. Member
**
Karma: 1
Posts: 80
Arduino in VS MODE
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

im trying to make my motor to run  only one lap wait and to run another lap but have some issue
Code:
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
Logged

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

United Kingdom
Offline Offline
Tesla Member
***
Karma: 223
Posts: 6593
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The following line:

Code:
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?
Logged

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.

Germany
Offline Offline
Jr. Member
**
Karma: 1
Posts: 80
Arduino in VS MODE
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

United Kingdom
Offline Offline
Tesla Member
***
Karma: 223
Posts: 6593
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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.

Germany
Offline Offline
Jr. Member
**
Karma: 1
Posts: 80
Arduino in VS MODE
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the home position is open
if the motor run the switch is closed
have modified the code
Code:
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
« Last Edit: October 29, 2012, 12:50:04 pm by THX_RoG » Logged

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

United Kingdom
Offline Offline
Tesla Member
***
Karma: 223
Posts: 6593
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

After the line

Code:
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:
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.
« Last Edit: October 30, 2012, 03:08:43 am by dc42 » Logged

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.

Germany
Offline Offline
Jr. Member
**
Karma: 1
Posts: 80
Arduino in VS MODE
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

heh its working +/-
Logged

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

Pages: [1]   Go Up
Jump to: