Probably a simpel question!

Hi..
Im pretty new to the Arduino.. but im learning :wink:

I have i switch(switchPin), and when i press it once the motor needs to run until it hits another switch(switch2Pin)... I thinks its simpel but I just cant figure it out!

I tried if/else statements, but the motor just stops when i release the switch.

if(digitalRead(switchPin) == HIGH) // start the motor
  {
   digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
   digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low

if(digitalRead(switch2Pin) == HIGH) // stop on the rotary part
  {
   digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge low
   digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low

I also tried with "buttonstate" but still i cant get it to work!

val = digitalRead(switchPin);  // When "load beer" button is pressed
  if (val != buttonState) {
  if (val == HIGH) {
   digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
   digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
   else {}
   buttonState = val;

Hope somebody out there can help me...

Can you paste your full code please? Both your code snippets above appear to have mismatched curly brackets but that might be just because we're not seeing the full sketch.

Also how are you connecting your switches to the Arduino? Are you using external pull up or pull down resistors (as described here: http://arduino.cc/en/Tutorial/Button )?

Andrew

It's probably best to separate your code into functions to make it easier to diagnose, and you might want to add a varaible to track the state of the motor

boolean motor_running = false;

void loop()
{
if (first_button_pushed && !motor_running)
start_motor();
if (second_button_pushed && motor_running)
stop_motor();
}

void start_motor()
{
// code to start motor
motor_running = true;
}

void stop_motor()
{
// code to stop motor
motor_running = false;
}

Back again! My computer blew up... But now back with a new one :sunglasses:

Can you paste your full code please? Both your code snippets above appear to have mismatched curly brackets but that might be just because we're not seeing the full sketch.

Also how are you connecting your switches to the Arduino? Are you using external pull up or pull down resistors (as described here: http://arduino.cc/en/Tutorial/Button )?

Andrew

Yep I have connected my switch using a pull up resistor. Im pretty sure the error is in my code. I know why it aint working, but I have no idea how to fix it

int backPin = 2;      // back input
int forwardPin = 3;   // forward input
int motor1Pin = 4;    // H-bridge leg 1  Rotation right
int motor2Pin = 5;    // H-bridge leg 2  Rotation left
int switchPin = 6;      // Loads the beer! GET READY!
int switch2Pin = 7;      // Stops the wheel
int motor3Pin = 8;    // H-bridge leg 3 LIFT
int motor4Pin = 9;    // H-bridge leg 4 LIFT


void setup() {
  // set the back/forward pins as an input:
  pinMode(backPin, INPUT); 
  pinMode(forwardPin, INPUT);
  pinMode(switchPin, INPUT);
  pinMode(switch2Pin, INPUT);

  // set all the other pins you're using as outputs:
  pinMode(motor1Pin, OUTPUT); 
  pinMode(motor2Pin, OUTPUT); 
  pinMode(motor3Pin, OUTPUT); 
  pinMode(motor4Pin, OUTPUT); 
  

}

void loop() {
{
  if (digitalRead(switchPin) == HIGH) // Switch button... Where it all goes wrong!!!
   digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
   digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
}
  if(digitalRead(switch2Pin) == HIGH) // stop on the rotation
  {
   digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge low
   digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
   delay(50);
   digitalWrite(motor3Pin, LOW);  
   digitalWrite(motor4Pin, HIGH);
  }

  if(digitalRead(backPin) == LOW && digitalRead(forwardPin) == LOW)
  {
    digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge low
    digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
  }
  else if(digitalRead(backPin) == HIGH && digitalRead(forwardPin) == LOW)
  {
    digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
    digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
  }
  else if(digitalRead(backPin) == LOW && digitalRead(forwardPin) == HIGH)
  {
    digitalWrite(motor1Pin, HIGH);   // set leg 1 of the H-bridge high
    digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge low
  }
}

Hope you smart guys out there can help me!

There is a bracket error at the beginning of the loop function:

void loop() [glow]{
{[/glow]
  if (digitalRead(switchPin) == HIGH) // Switch button... Where it all goes wrong!!!
   digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
   digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
}
 ......

Be sure not to delete one of the brackets, just move it as this:

void loop() {
  if (digitalRead(switchPin) == HIGH) {// Switch button... Where it all goes wrong!!!
   digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
   digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
}