helping with a push button

I have this following code for arduino robot and I want to program a button sensor. As you can see in the code the button sensor is connected to buttonPin = 12

so I want the robot to work when I press that button and stop when I press it again ( like a switch).

the code:

//Light seeker code

// connect motor controller pins to Arduino digital pins
// motor one
const int forcePin = A4;
const int buttonPin = 12;
int enA = 9;
int in1 = 2;
int in2 = 3;
// motor two
int enB = 10;
int in3 = 5;
int in4 = 4;
int sensorPin1 = A3;
int sensorPin2 = A1;
int sensorPinback = A5;

void setup()
{
// set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(sensorPin1, INPUT);
pinMode(sensorPin2, INPUT);
pinMode(sensorPinback, INPUT);
pinMode(buttonPin, INPUT);

Serial.begin(9600);
}

void demoOne()
{

// this function will run the motors in both directions at a fixed speed
// turn on motor A
int reading1 = analogRead(sensorPin1);

reading1 = map(reading1, 1000, 300, 60, 255);
reading1 = constrain(reading1, 60, 200);

int reading2 = analogRead(sensorPin2);

reading2 = map(reading2, 1000 , 300, 60, 255);
reading2 = constrain(reading2, 60, 200);

// print value to Serial monitor
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(enA, reading1);
// turn on motor B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(enB, reading2);
delay(100);

// now change motor directions

}

void demoThree()
{
// this function will run the motors in both directions at a fixed speed
// turn on motor A
int reading1 = analogRead(sensorPin1);

int reading2 = analogRead(sensorPin2);

int reading3 = analogRead(sensorPinback);

if(reading3<reading2 && reading3<reading1)
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(enA, 200);
// turn on motor B
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// set speed to 200 out of possible range 0~255
analogWrite(enB, 200);
delay(700);
}
}

void bumpsensor()
{
int reading3 = analogRead(forcePin);
if(reading3>20)
{
Serial.println(reading3); //Print value

// print value to Serial monitor
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// set speed to 200 out of possible range 0~255
analogWrite(enA, 150);
// turn on motor B
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// set speed to 200 out of possible range 0~255
analogWrite(enB, 150);
delay(3000);

digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

// turn on motor B
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

delay(700);

digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);

digitalWrite(in3, HIGH);
digitalWrite(in4, HIGH);
}

}

void loop()
{
int reading1 = analogRead(sensorPin1);
int reading2 = analogRead(sensorPin2);

while(reading1>100 && reading2>100)
{
demoOne();
demoThree();
bumpsensor();
}
}

Guys I really need help. I dont think that is so hard for professional like you.

Most people here are not Arduino professionals. The support you get depends on who happens to be reading the forum, and if they have the time or inclination to answer. If anybody has the ability and time to help, they will. Bumping your post after 30 minutes is not the way to encourage members to help.

Remember this is a forum of volunteers, like you, not of paid help desk agents.

Also remember it's international: not everyone is in your time zone. Lots of members are probably asleep right now, or driving their cars, or at work.... Sometimes you need to wait a whole day for the "right guy" to see your post and have the inclination and time to respond.

JimboZA:
Most people here are not Arduino professionals. The support you get depends on who happens to be reading the forum, and if they have the time or inclination to answer. If anybody has the ability and time to help, they will. Bumping your post after 30 minutes is not the way to encourage members to help.

Remember this is a forum of volunteers, like you, not of paid help desk agents.

Also remember it's international: not everyone is in your time zone. Lots of members are probably asleep right now, or driving their cars, or at work.... Sometimes you need to wait a whole day for the "right guy" to see your post and have the inclination and time to respond.

Thanks for the advice

Did you write that sketch?

Pretty much all the time that code runs is spent in one delay() or another.

The delay() allows nothing else to run during its time, except interrupts.

You could make buttons work using interrupts but still it will be like stutters with that sketch.

In my signature space just under this line are 3 Nick Gammon tutorial addresses.
The first one teaches about getting rid of delays, the lesson is invaluable for Arduino.

When you finish with that you may be ready to learn about button debouncing.

Why is it that every post that starts with "My switch isn't working..." fails to define how the switch is wired?

Hello.

Next time please use auto format text (ctrl+T) in arduino program and code bar in this forum. Your post will be much more easier to read.

I add a switch button code in your code. I don't know it's works or not, but I think so. Let me know the result. :wink:

//Light seeker code


// connect motor controller pins to Arduino digital pins
// motor one
const int forcePin = A4;
const int buttonPin = 12;
int enA = 9;
int in1 = 2;
int in2 = 3;
// motor two
int enB = 10;
int in3 = 5;
int in4 = 4;
int sensorPin1 = A3;
int sensorPin2 = A1;
int sensorPinback = A5;

int buttonState;
int act = 0;

void setup() {
  // set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(sensorPin1, INPUT);
  pinMode(sensorPin2, INPUT);
  pinMode(sensorPinback, INPUT);
  pinMode(buttonPin, INPUT);


  Serial.begin(9600);
}

void demoOne() {
  // this function will run the motors in both directions at a fixed speed
  // turn on motor A
  int reading1 = analogRead(sensorPin1);

  reading1 = map(reading1, 1000, 300, 60, 255);
  reading1 = constrain(reading1, 60, 200);

  int reading2 = analogRead(sensorPin2);

  reading2 = map(reading2, 1000 , 300, 60, 255);
  reading2 = constrain(reading2, 60, 200);


  // print value to Serial monitor
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  // set speed to 200 out of possible range 0~255
  analogWrite(enA, reading1);
  // turn on motor B
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  // set speed to 200 out of possible range 0~255
  analogWrite(enB, reading2);
  delay(100);

  // now change motor directions

}

void demoThree() {
  // this function will run the motors in both directions at a fixed speed
  // turn on motor A
  int reading1 = analogRead(sensorPin1);

  int reading2 = analogRead(sensorPin2);

  int reading3 = analogRead(sensorPinback);

  if (reading3 < reading2 && reading3 < reading1)
  {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    // set speed to 200 out of possible range 0~255
    analogWrite(enA, 200);
    // turn on motor B
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    // set speed to 200 out of possible range 0~255
    analogWrite(enB, 200);
    delay(700);
  }
}

void bumpsensor() {

  int reading3 = analogRead(forcePin);
  if (reading3 > 20)
  {
    Serial.println(reading3); //Print value


    // print value to Serial monitor
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    // set speed to 200 out of possible range 0~255
    analogWrite(enA, 150);
    // turn on motor B
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    // set speed to 200 out of possible range 0~255
    analogWrite(enB, 150);
    delay(3000);


    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);

    // turn on motor B
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);

    delay(700);

    digitalWrite(in1, HIGH);
    digitalWrite(in2, HIGH);

    digitalWrite(in3, HIGH);
    digitalWrite(in4, HIGH);
  }


}

void loop() {

  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH)
  {
    if (act == 0)
    {
      act = 1;
      int reading1 = analogRead(sensorPin1);
      int reading2 = analogRead(sensorPin2);

      while (reading1 > 100 && reading2 > 100)
      {
        demoOne();
        demoThree();
        bumpsensor();
      }
      Serial.println(act);
      delay(500);
    }
    else {
      act = 0;
      int reading1 = analogRead(sensorPin1);
      int reading2 = analogRead(sensorPin2);

      while (reading1 > 100 && reading2 > 100)
      {
        nodemoOne();
        nodemoThree();
        nobumpsensor();
      }
      Serial.println(act);
      delay(500);
    }
  }
}

void nodemoOne() {
  // this function will run the motors in both directions at a fixed speed
  // turn on motor A
  int reading1 = analogRead(sensorPin1);

  reading1 = map(reading1, 1000, 300, 60, 255);
  reading1 = constrain(reading1, 60, 200);

  int reading2 = analogRead(sensorPin2);

  reading2 = map(reading2, 1000 , 300, 60, 255);
  reading2 = constrain(reading2, 60, 200);


  // print value to Serial monitor
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  // set speed to 200 out of possible range 0~255
  analogWrite(enA, 0);
  // turn on motor B
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  // set speed to 200 out of possible range 0~255
  analogWrite(enB, 0);
  delay(100);

  // now change motor directions

}

void nodemoThree() {
  // this function will run the motors in both directions at a fixed speed
  // turn on motor A
  int reading1 = analogRead(sensorPin1);

  int reading2 = analogRead(sensorPin2);

  int reading3 = analogRead(sensorPinback);

  if (reading3 < reading2 && reading3 < reading1)
  {
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    // set speed to 200 out of possible range 0~255
    analogWrite(enA, 0);
    // turn on motor B
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
    // set speed to 200 out of possible range 0~255
    analogWrite(enB, 0);
    delay(700);
  }
}

void nobumpsensor() {

  int reading3 = analogRead(forcePin);
  if (reading3 > 20)
  {
    Serial.println(reading3); //Print value


    // print value to Serial monitor
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    // set speed to 200 out of possible range 0~255
    analogWrite(enA, 0);
    // turn on motor B
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
    // set speed to 200 out of possible range 0~255
    analogWrite(enB, 0);
    delay(3000);


    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);

    // turn on motor B
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);

    delay(700);

    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);

    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
  }


}

heninsh:
Hello.

Next time please use auto format text (ctrl+T) in arduino program and code bar in this forum. Your post will be much more easier to read.

I add a switch button code in your code. I don't know it's works or not, but I think so. Let me know the result. :wink:

//Light seeker code

// connect motor controller pins to Arduino digital pins
// motor one
const int forcePin = A4;
const int buttonPin = 12;
int enA = 9;
int in1 = 2;
int in2 = 3;
// motor two
int enB = 10;
int in3 = 5;
int in4 = 4;
int sensorPin1 = A3;
int sensorPin2 = A1;
int sensorPinback = A5;

int buttonState;
int act = 0;

void setup() {
  // set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(sensorPin1, INPUT);
  pinMode(sensorPin2, INPUT);
  pinMode(sensorPinback, INPUT);
  pinMode(buttonPin, INPUT);

Serial.begin(9600);
}

void demoOne() {
  // this function will run the motors in both directions at a fixed speed
  // turn on motor A
  int reading1 = analogRead(sensorPin1);

reading1 = map(reading1, 1000, 300, 60, 255);
  reading1 = constrain(reading1, 60, 200);

int reading2 = analogRead(sensorPin2);

reading2 = map(reading2, 1000 , 300, 60, 255);
  reading2 = constrain(reading2, 60, 200);

// print value to Serial monitor
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  // set speed to 200 out of possible range 0~255
  analogWrite(enA, reading1);
  // turn on motor B
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  // set speed to 200 out of possible range 0~255
  analogWrite(enB, reading2);
  delay(100);

// now change motor directions

}

void demoThree() {
  // this function will run the motors in both directions at a fixed speed
  // turn on motor A
  int reading1 = analogRead(sensorPin1);

int reading2 = analogRead(sensorPin2);

int reading3 = analogRead(sensorPinback);

if (reading3 < reading2 && reading3 < reading1)
  {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    // set speed to 200 out of possible range 0~255
    analogWrite(enA, 200);
    // turn on motor B
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    // set speed to 200 out of possible range 0~255
    analogWrite(enB, 200);
    delay(700);
  }
}

void bumpsensor() {

int reading3 = analogRead(forcePin);
  if (reading3 > 20)
  {
    Serial.println(reading3); //Print value

// print value to Serial monitor
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    // set speed to 200 out of possible range 0~255
    analogWrite(enA, 150);
    // turn on motor B
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    // set speed to 200 out of possible range 0~255
    analogWrite(enB, 150);
    delay(3000);

digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);

// turn on motor B
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);

delay(700);

digitalWrite(in1, HIGH);
    digitalWrite(in2, HIGH);

digitalWrite(in3, HIGH);
    digitalWrite(in4, HIGH);
  }

}

void loop() {

buttonState = digitalRead(buttonPin);

if (buttonState == HIGH)
  {
    if (act == 0)
    {
      act = 1;
      int reading1 = analogRead(sensorPin1);
      int reading2 = analogRead(sensorPin2);

while (reading1 > 100 && reading2 > 100)
      {
        demoOne();
        demoThree();
        bumpsensor();
      }
      Serial.println(act);
      delay(500);
    }
    else {
      act = 0;
      int reading1 = analogRead(sensorPin1);
      int reading2 = analogRead(sensorPin2);

while (reading1 > 100 && reading2 > 100)
      {
        nodemoOne();
        nodemoThree();
        nobumpsensor();
      }
      Serial.println(act);
      delay(500);
    }
  }
}

void nodemoOne() {
  // this function will run the motors in both directions at a fixed speed
  // turn on motor A
  int reading1 = analogRead(sensorPin1);

reading1 = map(reading1, 1000, 300, 60, 255);
  reading1 = constrain(reading1, 60, 200);

int reading2 = analogRead(sensorPin2);

reading2 = map(reading2, 1000 , 300, 60, 255);
  reading2 = constrain(reading2, 60, 200);

// print value to Serial monitor
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  // set speed to 200 out of possible range 0~255
  analogWrite(enA, 0);
  // turn on motor B
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  // set speed to 200 out of possible range 0~255
  analogWrite(enB, 0);
  delay(100);

// now change motor directions

}

void nodemoThree() {
  // this function will run the motors in both directions at a fixed speed
  // turn on motor A
  int reading1 = analogRead(sensorPin1);

int reading2 = analogRead(sensorPin2);

int reading3 = analogRead(sensorPinback);

if (reading3 < reading2 && reading3 < reading1)
  {
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    // set speed to 200 out of possible range 0~255
    analogWrite(enA, 0);
    // turn on motor B
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
    // set speed to 200 out of possible range 0~255
    analogWrite(enB, 0);
    delay(700);
  }
}

void nobumpsensor() {

int reading3 = analogRead(forcePin);
  if (reading3 > 20)
  {
    Serial.println(reading3); //Print value

// print value to Serial monitor
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    // set speed to 200 out of possible range 0~255
    analogWrite(enA, 0);
    // turn on motor B
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
    // set speed to 200 out of possible range 0~255
    analogWrite(enB, 0);
    delay(3000);

digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);

// turn on motor B
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);

delay(700);

digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);

digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
  }

}

The button worked perfectly for making the robot start. However, the robot doesn't stop when you press it again.
so if anyone can help with that that would be perfect.

Thank you very much,

Did you upload last code after I edit my last post? Code in my first post was wrong. It did same thing in if and else function. But now it must work.

heninsh:
Did you upload last code after I edit my last post? Code in my first post was wrong. It did same thing in if and else function. But now it must work.

Yes I tried. When I try to turn it off using the button it doesn't work.

kmarhoumi:
The button worked perfectly for making the robot start. However, the robot doesn't stop when you press it again.
so if anyone can help with that that would be perfect.

Thank you very much,

You have some things to learn about coming up, they will be good for you to know so don't avoid!

The delay() command allows nothing else to run until the time is up, yes? Not even pin reading needed to detect button press. You have lot of code time spent in delays while the rest of the code runs in almost no time at all -- meaning almost all of your sketch run time does nothing.

Button read only happens when the sketch reads the pin or the pin is set to interrupt and even then the sketch only reacts when the sketch gets to the code to react.

Code that runs without delay() or other forced-wait functions has cycles do the reads and reacts many times per millisecond (button push code, react to interrupt, etc, under-1-ms-responsive), so fast your code can see button contact bounce to have to code for.

In my signature space at the bottom of my post, try the 1st and 2nd tutorials at those addresses, 3rd if you want to understand interrupts. He has a whole easy study on button reading on the same site.