Hey guys !
I am new to Arduino, and i want to control a DC motor through a webserver (on Arduino).
So, as i said, i'm trying to control a high power DC motor, to go in both directions. I already built my very own H-bridge in order to drive the motor forwards and in reverse by using 2 digital outputs for each direction.
My problem is that i have to use some kind of delay, to control the distance for which my motor runs. For example, i press an html "on" button, and my dc spins for.. say 6 seconds forwards. Then, i want to run it backwards also for 6 seconds, through the same button (with an inverted label "off"). The thing is, i don't know how to do that without using the "delay()" function (because it stalls the serial communication).
The code i would need would be something like this:
const int motorUP= 5;
const int motorDOWN = 6;
void motorUpFct(){
** digitalWrite(motorUP, HIGH);**
** delay(6000);**
** digitalWrite(motorUP,LOW);**
}
void motorDownFct(){
** digitalWrite(motorDOWN, HIGH);**
** delay(6000);**
** digitalWrite(motorDOWN,LOW);}**
, and when changing the URL to smth like "?Motor=motorON", the motorUpFct would be called, and the other way around...
I also checked out the "blink without delay", but i can't seem to make it work.
Thanks for your time, and any help is well appreciated!
My problem is that i have to use some kind of delay, to control the distance for which my motor runs.
No you don't.
I also checked out the "blink without delay", but i can't seem to make it work.
Without showing us what you tried, we can hardly tell you what you did wrong.
Two of the most common replies apply to you question.
-
Use code tags when posting code!
-
See blink without delay (examples) then look in the playground.
The other topic you need to look at is the Finite State Machine, again its in the playground.
Mark
Thanks to you both, i'll continue trying, and also look into the 'finite state machine', and come back with a reply.
P.S. Also, i specified the fact that i looked into the 'blink without delay', and i'll use code tags from now on.
P.S. Also, i specified the fact that i looked into the 'blink without delay',
True, but you didn't say what problems you had understanding/implementing what you learned.
Hello again!
I've checked the state machine programming, and put together some code. I managed to make the motor "go up" for 6 seconds when starting arduino, and after pressing a button, it "goes down" for 6 seconds. The problem i have now is that it doesn't reinitialize. So it turns up once automatically, i can turn it down once manually, but when pressing the button again, it should again go up, and so on. Well.. it stops after the first two.
I think the problem's in the if statement found in the Wait4Press state, or with those mstatus variables. Here's my code till now.
#include <SM.h>
#define pin12 12
#define pin13 13
#define sw 2
SM ArduinoOn(On);
int mstatus;
//if mstatus = 1 => motor has gone up
//if mstatus = -1=> motor has gone down
void setup()
{
pinMode(pin12, OUTPUT);
pinMode(pin13, OUTPUT);
pinMode(sw, INPUT);
// Serial.begin(9600);
}
void loop()
{
EXEC(ArduinoOn);
}
State On()
{
// Serial.println("Arduino ON, going to Initialize");
if (ArduinoOn.Timeout(1000))
{
ArduinoOn.Set(Initialize);
}
}
State Initialize()//initialize will turn the motor up for 6 sec
{
// Serial.println("Arduino initialized, stopping action, storing monitorOn var");
// Serial.println(" motor variable is now");
// Serial.print(mstatus);
digitalWrite(pin13, HIGH);
mstatus = 1;
if (ArduinoOn.Timeout(6000))
{
ArduinoOn.Set(StopUpAction);
}
}
State StopUpAction()
{
digitalWrite(pin13, LOW);
mstatus = 1;
ArduinoOn.Set(Wait4Press);
}
State Wait4Press()
{
if ( (digitalRead(sw) == HIGH) && mstatus == 1) //if the button is pressed and the motor is UP, pull it down
{
ArduinoOn.Set(GoDownAction);
if ((digitalRead(sw) == HIGH) && mstatus == -1)//if the button is pressed and the motor is DOWN, pull it up
{
ArduinoOn.Set(Initialize);
}
}
}
State GoDownAction()
{
digitalWrite(pin12, HIGH);
mstatus = -1;
if (ArduinoOn.Timeout(6000))
{
ArduinoOn.Set(StopDownAction);
}
}
State StopDownAction()
{
digitalWrite(pin12, LOW);
mstatus = -1;
// Serial.println("action stopped, var is now");
// Serial.print(mstatus);
ArduinoOn.Set(Wait4Press);
}
I've also tried an if/else statement, but had the same result.
Thanks for your help, and sorry for the hold up, but i have exams.
In my opinion it's a pity you got drawn to the State Machine library. To my way of thinking that is a "black box" that just makes a simple thing seem complicated.
A state machine is just a concept in which the various states that the system can have are stored in one or more variables. It can be part of the simplest of sketches.
I wrote an extended demo of the Blink Without Delay example sketch in the first post in this Thread. It uses the state machine concept without any need for a library.
...R
Thank you, i'll have a look at your code, and i'm interested in more suggestions.
I got it working by including another "Wait4Press" state. So now i have 2 states that are waiting for input, each for every action..