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