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. I
m 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!
+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?
Okay I didnt know that I coulden
t use an if statment on things that arent on a PIN. Haven
t 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
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 dosen
t 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);
}
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);
}
}
}
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");
}
}