button state with dc motor

Hello fellow boys and girls

Im trying to make a dc motor and a LED to turn on when I press the button, it shall stay on until I press the button again. This works with the LED light, but the dc motor wont. Im steering the dc motor with an arduino motor shield.

The code won`t compile because of the last sentence in the code, if I take it out of the code
it will compile, the only thing is that the engine will only run once.

This is the last sentence:
digitalWrite(Engine1, Engine1controller);

const int Button1   = 2;
const int Led1     = 13;
int buttonState    = A0;


#include <AFMotor.h>
AF_DCMotor Engine1(2);

int Led1controller = LOW;
int Engine1controller = LOW;

void setup()
{
  Serial.begin(9600);

  pinMode(Led1, OUTPUT);
  pinMode(Button1, INPUT);

  Engine1.setSpeed(200);
  Engine1.run(RELEASE);
}

void loop()
{


  while (digitalRead(Button1) == LOW);

  if (digitalRead(Button1) == HIGH)
  {

    Led1controller = !Led1controller; //ledstyring = LED steering
    Engine1controller = !Engine1controller; //Motor styring = Motor steering

    delay(2000);


    buttonState = digitalRead(Button1);

    digitalWrite(Led1, HIGH);

    Engine1.run(FORWARD);
    {
      Engine1.setSpeed(250);
      delay(2000);
    }

    Engine1.run(BACKWARD);
    {
      Engine1.setSpeed(250);
      delay(2000);
      Engine1.run(RELEASE);
    }
  }

  while (digitalRead(Button1) == HIGH);
  digitalWrite(Led1, Led1controller); //This works

  delay(2000);
  //digitalWrite(Engine1, Engine1controller); THIS IS WHERE I GET MY ERROR MESSAGE!

}

Thank you so much for your help.

Best regards!

Engine1 isn't a pin number, so you can't digitalWrite to it. The AF_DCMotor class has methods to make the motor spin, you have to use those methods. You appear to have written them in other places in the code. What you need is an if statement that checks the state of the button and calls one of those methods as appropriate.

+1 to what Delta_G wrote.

I know Engine1 isn`t a pin number. The engine1 is set to the motor1 input on the motor shield and that works.
I also have an if statment that checks the signal of the button. If the signal is high, it should turn on LED light and start motor first forward then backward and repeat that in the while loop until I trigger the button again.

Everything works well except that motor. The light goes on and of with the button when I take the last sentence out of the code.
I can`t seem to understand what you guys mean.

I apprechiate the answer and hope you guys can help me one more time, the best would be if you could rewrite the code where it`s necessary, I know that is work, but I would highly apprechiate it.

Best regards

arduinofan90:
I`m trying to make a dc motor and a LED to turn on when I press the button, it shall stay on until I press the button again.

Do you want the button to toggle the motor on and off?

OK, so maybe you don't understand what a while statement does or how to use one.

So lets read through your loop code and see if it makes sense what it does.

while (digitalRead(Button1) == LOW);

Do nothing, and keep repeating doing nothing so long as the button reads low.

Then, if the button reads HIGH, change a couple of variables and delay for 2 seconds.
Next read the state of the button into the variable buttonState which is never used again.

Turn the led on

run the motor forward for 2 seconds

run the motor backwards for 2 seconds

Do nothing and continue doing nothing as long as the button is HIGH

Once the button is released write the LED to the state determined earlier

wait 2 seconds

WHAT DO YOU WANT TO HAPPEN HERE????

Go back to the top of the loop and do nothing until the button is pressed again.

If you want to run the motor in response to that variable Engine1controller, then you'll have to use an if statement:

if(Engine1controller == HIGH){
    Engine1.run(FORWARD);
}
else {
   Engine1.run(BACKWARDS);
}

or something similar depending on what you actually want to happen in response to that variable.

You can't use digitalWrite on something that isn't a pin number.

Okay I didnt know that I couldent use an if statment on things that arent on a PIN. Havent much experience with motor shield.

I want nothing to happen, until I press the button.

When I press the button I want the light to go on, and motor start running forward for two secounds, and then backward for two secounds (the timing isn`t set yet for my project, I will figure that out soon.)
I want this motor to repeat this sequence and the light should stay on until I press the button again, the motor should stop and light should go off.

Thank you!

Best regards

You can use an if statement for things that aren't a pin. You can't use digitalWrite with something that isn't a pin. How did my reply sound like you couldn't use an if statement?

If you want it to keep repeating until you press the button again, then having it sit in a while loop doing nothing doesn't sound like a good idea. Maybe on the button press you can set a flag and then have an if statement that checks that flag and decides whether or not to run the motor and light the led.

Pseudocode:

boolean flag = false;

void loop() {
  if(the button changes state to pressed){
     flag = !flag;
  }
  if(flag){
     do the motor and led stuff
  }
}

Never used flag, before. I re wrote my code. Now the engine is working.
It start foreward, then backward and stays there. It dosent make the while loop again.
I honestly cant find out why that dosent work.

The lights goes off after I press the button for the secound time and not the engine, she is just running backward and never stops.

I feel very stupid, but I`m new to the button thing and motorshield.
Thank you so much for helping me out. I really apprechiate it.

Here is my code now:

const int Button1   = 2;
const int Led1     = 13;
int buttonState    = A0;

#include <AFMotor.h>
AF_DCMotor Engine1(2);

int Led1controller = LOW;
int Engine1controller = LOW;

void setup()
{
  Serial.begin(9600);
  pinMode(Led1, OUTPUT);
  pinMode(Button1, INPUT);
}

void loop()
{

  while (digitalRead(Button1) == LOW);

  if (digitalRead(Button1) == HIGH)
  {

    buttonState = digitalRead(Button1);
    Led1controller = !Led1controller; 
    Engine1controller = !Engine1controller; 

    digitalWrite(Led1, HIGH);

(Engine1controller == HIGH);
    
    Engine1.run(FORWARD);
    {
      Engine1.setSpeed(250);
      delay(2000);
    }
    Engine1.run(BACKWARD);
      {
      Engine1.setSpeed(250);
      delay(2000);

      }


  }



while (digitalRead(Button1) == HIGH);
  digitalWrite(Led1, Led1controller);
  ( Engine1controller== LOW);

}

It doesn't repeat because you told it to stop here and do nothing until you press that button again.

while (digitalRead(Button1) == LOW);

And you tell it to stop here and do nothing until you release the button.

while (digitalRead(Button1) == HIGH);

If you want that code to repeat, you can't lock it up in an empty while loop. Let the loop function repeat over and over. Study my pseudocode above and see if you can write code to match it. It isn't hard.

Nice, now the engine is running back and fourth.
The clock is 2 AM here now, so I think I`ll go to sleep.

The last part now is to get the LED and engine off when pressing the button again.
I tried diffrent things but it dosen`t work.

When I`m done with this I will never ask for anything more. You have been so kind to me, thank you very much!

Here is my new code:

const int Button1   = 2;
const int Led1     = 13;
int buttonState    = A0;

#include <AFMotor.h>
AF_DCMotor Engine1(2);

int Led1controller = LOW;
int Engine1controller = LOW;

void setup()
{
  Serial.begin(9600);
  pinMode(Led1, OUTPUT);
  pinMode(Button1, INPUT);

}

  boolean flag = false;

void loop()
{


  if(digitalRead(Button1) == HIGH)
  {
     flag = !flag;
    
  }
  if(flag)
  {
  
    buttonState = digitalRead(Button1);
    Led1controller = !Led1controller; 
    Engine1controller = !Engine1controller; 

    digitalWrite(Led1, HIGH);

    (Engine1controller == HIGH);
    
    Engine1.run(FORWARD);
    {
      Engine1.setSpeed(250);
      delay(2000);
    }
    Engine1.run(BACKWARD);
      {
      Engine1.setSpeed(250);
      delay(2000);
     }


    
        }

}

You still have a bunch of useless code in there.

Can you explain what you think Engine1controller is doing for you? You keep twiddling with the value of it, but you never use it anywhere. Same with buttonState, you never use it. ANd at the top you have it defined as A0? Now is it a button state or a button pin?

Also, you probably don't want to swap your flag when the button IS high. The loop might turn over a hundred times or more while you have your finger on the button. You want it to change when the button first becomes pressed. Have a look at the "State Change" example that comes with the IDE for some inspiration there. You only want to switch your flag when the button is pressed now but wasn't the last time you looked at it.

I rewrote my code again, everything is working now, except when I press the button for the secound time it dosen`t stop, it just continues in the loop.
This was so simple without this motorshield…

Now I`m getting desperate for help, can someone please help me out big time?

Thank you very much, I`ll highly apprechiate it!

const int Button1   = 2;
const int Led1     = 13;
int Led1controller = LOW;
int Engine1controller = LOW;
int buttonState = 0;        
int lastButtonState = 0;   
#include <AFMotor.h>

AF_DCMotor Engine1(2);

void setup() 
{
pinMode(Button1, INPUT);
 pinMode(Led1, OUTPUT);
 Serial.begin(9600);
 while (digitalRead(Button1) == LOW); //Do nothing until I press the button
}

void loop()
{
buttonState = digitalRead(Button1);

if (buttonState = !lastButtonState) //The button is pressed, buttonstate is not equal to last position
  
  {     
      
      digitalWrite(Led1, HIGH); //Turn on LED light

    (Engine1controller == HIGH); 
    
    Engine1.run(FORWARD);
    {
      Engine1.setSpeed(250);
      delay(2000);
    }
    Engine1.run(BACKWARD);
      {
      Engine1.setSpeed(250);
      delay(2000);
      }

      Serial.println("The LED and motor is now in it`s loop");
    }
  
    else if (buttonState = lastButtonState) //I press the button again, the buttonstate is NOT equal to last buttonstate so it will go in here
        {
       digitalWrite(Led1, LOW);       //Turn LED off
       (Engine1controller == LOW);    //Turn off Engine
       Serial.println("The LED and motor has stopped");
      }

}