button, motor, shield

Hello

Im nearly to complete my little project, but the arduino shield is new to me. The program is ment to do nothing until i press the button, the it should turn on LED lights and motor should start running forward, then backward and the motor and LED should do that inside the loop until I press the utton again, then it all should stop. Everything works, except that the motor and LED dosent turn off after I push the button.
I have tried many things without luck, and I hope somebody could give me the answer because I`m so tired of this little problem that messes up my holde thing! Please!
I apprechiate it, best regards:

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");
      }

}

Hello,

You have several syntax errors in your code.

This:

if (buttonState = !lastButtonState) //The button is pressed, buttonstate is not equal to last position

should be:

if (buttonState != lastButtonState) //The button is pressed, buttonstate is not equal to last position

this:

(Engine1controller == HIGH);

should be:

if (Engine1controller == HIGH)
{

or:

Engine1controller = HIGH;

This:

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

should be:

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

Maybe others of the same kind.

Also you should not use delays if you want your code to respond accurately to button presses. Look at the Blink Without Delay example.

To stop the motor:-

Engine1.run(RELEASE);

These do nothing:-

(Engine1controller == HIGH);
(Engine1controller == LOW);

They're in no way connected to the motor, and even if they were, you would use "=", not "==".
And you don't need brackets around them, either. Edit: Unless, as guix says, they're combined with 'if' statements. In that case, "==" is correct.

@guix, I interpreted things differently, but basically we're coming from the same place.

@arduinofan90, you really need to go and study the language a bit more. You're a long way from achieving what you want.
Check out a few examples and see how they're put together.

Yes I started a new thread, its not showing up on my computer, but your link worked, sorry about that. As I said, got it working without motor shield. This is new to me and yes Im not that good with motor shield and !lastbutton command.
But I have programmed a small scale greenhouse that did alot of things. I came here to get some help, and Im very glad so far for everything. But this thing is a disaster, everything is working except the motor and led that wont turn off so I have done something right.
I dont know what to do, I agree there were som errors on the code I shared last time, that code was made after frustration and anger, so It wasent that good.
Now I dont know what to do, Its so sad because I don`t have much time to finish it.

Sorry I`m not so good at this, and ask alot of questions.

best regards

arduinofan90:
Yes I started a new thread, its not showing up on my computer, but your link worked, sorry about that. As I said, got it working without motor shield. This is new to me and yes Im not that good with motor shield and !lastbutton command.
But I have programmed a small scale greenhouse that did alot of things. I came here to get some help, and Im very glad so far for everything. But this thing is a disaster, everything is working except the motor and led that wont turn off so I have done something right.
I dont know what to do, I agree there were som errors on the code I shared last time, that code was made after frustration and anger, so It wasent that good.
Now I dont know what to do, Its so sad because I don`t have much time to finish it.

Sorry I`m not so good at this, and ask alot of questions.

best regards

Again,

OldSteve:
you really need to go and study the language a bit more. You're a long way from achieving what you want.
Check out a few examples and see how they're put together.

Engine1controller is not doing a thing. I dont think you guys are so helpful. I try to make a code for a little project I have, when I read the feedback from you I feel that I suck at this. I know I'm not good, but hey everyone got to start somewhere. The reason I'm here is because I need help.
If you guys are so good why dont just give me something that works and explain it to me when you see me fail over and over again.
And no the last post I had is invisible to me cant find it unfortunality, then I would have continued there.

Now I have a feeling that I dont like arduino and might just skip it and find a new hobby.

arduinofan90:
Engine1controller is not doing a thing. I dont think you guys are so helpful. I try to make a code for a little project I have, when I read the feedback from you I feel that I suck at this. I know I'm not good, but hey everyone got to start somewhere. The reason I'm here is because I need help.
If you guys are so good why dont just give me something that works and explain it to me when you see me fail over and over again.
And no the last post I had is invisible to me cant find it unfortunality, then I would have continued there.

Now I have a feeling that I dont like arduino and might just skip it and find a new hobby.

I find this post just plain rude. We are not paid teachers, and you have absolutely no right to suggest that we should write your code for you. Remember, we are just fellow Arduino users. You should be thankful that we are helping you at all, rather than griping that we're not helping enough. You have an attitude problem, in my opinion.

Our whole aim is to guide beginners, so that they are able to learn. We all made good suggestions that you ignored.
Before doing anything in life, you first have to learn how. With Arduino, if you're not prepared to enroll in a C++ course and learn properly, the next best way is to study examples and other people's code, read as much reference material as you can, and then start by writing very basic programs that flash LEDS, read buttons etc.
Use those programs to test out the things that you learn in your reading. Then, when you feel that you have a good grasp of the basics, move on to more complex things.

It's true that I said this does nothing.
(You'd already been told this more than once, too.):-

(Engine1controller == LOW);

Did I not also tell you how you should stop the motor?:-

Engine1.run(RELEASE);

And guix gave you good advice, too.
Delta_G has also given you very good advice on this subject, now spanning two threads.

Despite what you think, we have all been helping. But you don't appear to be trying to help yourself. And your poor attitude in your last post will only ensure that you receive no more help from us, (or from me, at least).

In summary, go and learn a little more about the language, then revisit your project.
(Perhaps you could start by studying the AFMotor examples carefully, and then search the forums for more examples of people's motor control code using those modules.)
Good luck with it.

Maybe you would have better luck if you did a bit more research and showed a bit more appreciation.
Quitters never win and winners never quit.

But this thing is a disaster, everything is working except the motor and led that won`t turn off so I have done something right.

Try this

Use it EXACTLY as you see. Don't change ANYTHING. Test it and make sure the led goes off when you toggle it. If it works, just add your motor code wherever you see the code to turn on or off the led.
Turn on the motor when the led turns on and turn the motor off when the led turns off.

Im so sorry for bitching to you. I didnt think straight, I had a coupple of tough days.
I know its not making up for what I did, but I just wanted you to know, Im so sorry.

I really apologize to all of you!

I tried to get this one https://www.arduino.cc/en/Tutorial/Switch working, but
I werent smart enough. The light gos on and off but the motor dosent act as
it should.

I made a new code with cases: (This one is working except the motor is going forward then backwards and does not go forward again, It just goes back back back and back. If I press the button, everything stops so that`s working:)

/*
Når program starter, ingenting skal skje. 
 
Når knapp blir trykket, lys går på, motor går forover 
2 sekunder, bakover 2 sekunder og repeterer dette samtidig som at
lyset er på INNTIL knapp blir trykket igjen og alt skal stoppe.
*/
 
 
#include <AFMotor.h>
 
const int Button1   = 2;
const int Led1     = 13;
int knapp;
 
AF_DCMotor Engine1(2);
 
void setup() 
  {
    Serial.begin(9600);
    pinMode(Led1, OUTPUT);
    pinMode(Button1, INPUT);  
 }
 
void loop() 
 {
 
    if (digitalRead(Button1) == HIGH)                                    
 {
  if(knapp == 0)
  {
 
  knapp=1;
  ON();
  }
  else
  { 
  knapp=0;
  OFF();       
 }
}
 }
 
void ON()       
{
  Serial.print("Motor og lys er på!");
 
digitalWrite(Led1, HIGH);
 
  
    Engine1.run(FORWARD);
    {
      Engine1.setSpeed(250);
      delay(2000);
    }
    Engine1.run(BACKWARD);
      {
      Engine1.setSpeed(250);
      delay(2000);
      }
}
 
 
void OFF()
{
  
Engine1.run(RELEASE);      // RELEASE BETYR MOTOR: STOPP OG KJØR!
digitalWrite(Led1, LOW);
delay(1000);  
}

And again, I`m really sorry being a jackass!
best regards

The program is ment to do nothing until i press the button, the it should turn on LED lights and motor should start running forward, then backward and the motor and LED should do that inside the loop until I press the utton again, then it all should stop.

I gave you a link that would work if you followed my instructions.

Post the code to make the motor start .
Post another sketch to make the motor stop.
Put the two together into one sketch with a 1 second delay in between.
Then come back, post that sketch and we'll go from there.

You might try using a boolean flag for the button state and read that in the loop .

First of all, thank you for giving me a secound chance. That`s very kind of you.

I copied the switch code to arduino, and I think it could be nice to use the cases I`ve made.

It is working except I have to push the button a coupple of times before it stops and starts again.
Maybe I can do something to make it stop when I push the button the forst time and it wil start when I push it after that.

int button = 2;         // the number of the input pin
int LED = 13;       // the number of the output pin
#include <AFMotor.h>
AF_DCMotor Engine1(2);
 
int state = LOW;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(button, INPUT);
  pinMode(LED, OUTPUT);
}

void loop()
{
  reading = digitalRead(button);


  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

if (state=state)
{
  digitalWrite(LED, state);

  ON();
delay(3000);
  previous = reading;
}
else
{
OFF();
  digitalWrite(LED, LOW);
delay(3000);
}
}




void ON()       
{

  
    Engine1.run(FORWARD);
    {
      Engine1.setSpeed(250);
      delay(2000);
    }
    Engine1.run(BACKWARD);
      {
      Engine1.setSpeed(250);
      delay(2000);
      }
}
 
 
void OFF()
{
  
Engine1.run(RELEASE);      // RELEASE BETYR MOTOR: STOPP OG KJØR!
  delay(2000);
}
int button = 2;         // the number of the input pin
int LED = 13;       // the number of the output pin
#include <AFMotor.h>
AF_DCMotor Engine1(2);
 
int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(button, INPUT);
  pinMode(LED, OUTPUT);
}

void loop()
{
  reading = digitalRead(button);


  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
    {
      state = LOW;
      OFF;
    }
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(LED, state);
  ON;
  

  

  previous = reading;
}




void ON()       
{

  
    Engine1.run(FORWARD);
    {
      Engine1.setSpeed(250);
      delay(2000);
    }
    Engine1.run(BACKWARD);
      {
      Engine1.setSpeed(250);
      delay(2000);
      }
}
 
 
void OFF()
{
  
Engine1.run(RELEASE);      // RELEASE BETYR MOTOR: STOPP OG KJØR!
  delay(2000);
}

The motor dosen`t start with that code unfortunality:(
The light goes on and off, but nothing more..

/* switch
 * 
 * Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
 * press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
 * a minimum delay between toggles to debounce the circuit (i.e. to ignore
 * noise).  
 *
 * David A. Mellis
 * 21 November 2006
 */
int count =3600;
int inPin = 4;         // the number of the input pin
int outPin = 13;       // the number of the output pin
boolean runmotor = false;
unsigned int microsBetweenSteps = 2000;
int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
byte dirPin = 2;
byte stepPin = 3;
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
   Serial.begin(9600);
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
}

void loop()
{
             reading = digitalRead(inPin);

             // if the input just went from LOW and HIGH and we've waited long enough
             // to ignore any noise on the circuit, toggle the output pin and remember
             // the time
             
            if (reading == HIGH && previous == LOW && millis() - time > debounce) 
            
            {
              if (state == HIGH)
                {
                  state = LOW;
                  runmotor = false;
                   Serial.println("OFF");
                 digitalWrite(dirPin, LOW);
                digitalWrite(stepPin, LOW);
               // OFF();
               }
               
              else
              
               {
                state = HIGH;
                runmotor = true;
                time = millis();    
                }
            }

           digitalWrite(outPin, state);
           //moveMotor();
           Serial.println("ON");
             // PUT MOTOR ON CODE HERE
          ON();
          previous = reading;
}

   void ON()       
   {
    moveMotor();
    }
 
 
     void moveMotor() 
     {
      motorStep();
      }

     void motorStep()
      {
         if (runmotor == true)
            {
               for (int i=0; i< count; i++)
                {
                 digitalWrite(dirPin, HIGH);
                 digitalWrite(stepPin, LOW);
                 digitalWrite(stepPin, HIGH);
                 delayMicroseconds(microsBetweenSteps);
                 }
                 runmotor = false;
                 state = !state;
                 digitalWrite(outPin, state);
                  OFF();
             }
        }
        
          void OFF()
           {
            digitalWrite(dirPin, LOW);
            digitalWrite(stepPin, LOW);
           }

This code uses a variable named "count" which hold the number of steps the motor moves each time you press the button. (default is 3600, one revolution). When you press the button the LED turns on while the motor is moving and at the end of [count] steps, the motor stops and the led turns off.
That's the best I can do . If that doesn't work for you , ask someone else. Next time think twice about what you are about to post. Use the "Preview" button and ask yourself "am I going to piss anyone off with this post ?"

/* switch
 * 
 * Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
 * press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
 * a minimum delay between toggles to debounce the circuit (i.e. to ignore
 * noise).  
 *
 * David A. Mellis
 * 21 November 2006
 */
int count =3000;
int inPin = 4;         // the number of the input pin
int outPin = 13;       // the number of the output pin
boolean runmotor = false;
unsigned int microsBetweenSteps = 2000;
int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
byte dirPin = 2;
byte stepPin = 3;
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
   Serial.begin(9600);
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
}

void loop()
{
             reading = digitalRead(inPin);

             // if the input just went from LOW and HIGH and we've waited long enough
             // to ignore any noise on the circuit, toggle the output pin and remember
             // the time
             
            if (reading == HIGH && previous == LOW && millis() - time > debounce) 
            
            {
              if (state == HIGH)
                {
                  state = LOW;
                //  Serial.println("OFF");
               }
               
              else
              
               {
                state = HIGH;
                runmotor = true;
                digitalWrite(outPin, state);
                Serial.println("ON");
                ON();
                time = millis();    
                }
            }

          previous = reading;
}

   void ON()       
   {
    moveMotor();
    }
 
 
     void moveMotor() 
     {
      motorStep();
      }

     void motorStep()
      {
         if (runmotor == true)
            {
               Serial.println("FORWARD");
               for (int i=0; i< count; i++)
                {
                 digitalWrite(dirPin, HIGH);
                 digitalWrite(stepPin, LOW);
                 digitalWrite(stepPin, HIGH);
                 delayMicroseconds(microsBetweenSteps);
                 }
                Serial.println("REVERSE"); 
                 for (int i=0; i< count; i++)
                {
                 digitalWrite(dirPin, LOW);
                 digitalWrite(stepPin, LOW);
                 digitalWrite(stepPin, HIGH);
                 delayMicroseconds(microsBetweenSteps);
                 }
                 
                 
                 runmotor = false;
                 state = !state;
                 digitalWrite(outPin, state);
                 runmotor = false;
                 Serial.println("OFF");
                 OFF();
             }
        }
        
          void OFF()
           {
            digitalWrite(dirPin, LOW);
            digitalWrite(stepPin, LOW);
           }

That should do it. Look at the Serial Monitor output. The above is my code for my motor.
The following is your code for your motor driver:

int inPin = 2;         // the number of the input pin
int outPin = 13;       // the number of the output pin
#include <AFMotor.h>
AF_DCMotor Engine1(2);
 boolean runmotor = false;
int state = LOW;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  Serial.begin(9600);
 pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
}

void loop()
{
            reading = digitalRead(inPin);

             // if the input just went from LOW and HIGH and we've waited long enough
             // to ignore any noise on the circuit, toggle the output pin and remember
             // the time
             
            if (reading == HIGH && previous == LOW && millis() - time > debounce) 
            
            {
               if (state == HIGH)
                {
                  state = LOW;
                 //  Serial.println("OFF");
                 }
               
                else
              
                 {
                  state = HIGH;
                  runmotor = true;
                  digitalWrite(outPin, state);
                  Serial.println("ON");
                  ON();
                  time = millis();    
                 }
             }

             previous = reading;
}




void ON()       
{
       if (runmotor == true)
            {
                Serial.println("FORWARD");
                
                 Engine1.run(FORWARD);
                 {
                  Engine1.setSpeed(250);
                  delay(2000);
                  }
                  
                 Serial.println("REVERSE");  
                 Engine1.run(BACKWARD);
                 {
                  Engine1.setSpeed(250);
                  delay(2000);
                 }
                 
               
                 runmotor = false;
                 state = !state;
                 digitalWrite(outPin, state);
                 runmotor = false;
                 Serial.println("OFF");
                 OFF();
            }
}
 
 
void OFF()
{
  
Engine1.run(RELEASE);      // RELEASE BETYR MOTOR: STOPP OG KJØR!
  delay(2000);
}

"The following your code for your motor driver" is not working perfect. It goes ON forward, backwards and then stops. It should continue the forward backward until I press the button again.

Anyway, thanks:)