Hi everyone, I'm new to arduino but have spent much of the past year trying to learn in order to resolve this pet project of mine through doing different tutorials etc. but I think I might be in need of some direction, I'm happy to do the research and learning but I need to know what to focus on.
So the project outline is as follows...
When a switch is pressed, I need a 12v motor to be driven for about 2 seconds, if possible an LED to flash during this step, then an electro magnet kicks in to hold the plunger in place, the same LED goes solid to indicate it is engaged, everything waits in this state until the switch is pressed again at which point everything needs to ready itself to do the same sequence again.
The hardware I am using is an arduino uno board with a 12v dual motor driver (I hope I did my research correctly and that a motor driver is the correct thing to drive an electro magnet).
It seems that the hardware works fine because I setup a simple sketch that drove the motor for 2 seconds and then flipped over to the magnet - so that seems fine
The sketch is the problem area for me currently, I can't seem to get the order of things right.
Anything I do flips through the loop constantly so the motor comes on for 2 seconds, the magnet clicks and then it does it again over and over - which makes sense because it is looping - duh!
So please can I have some suggestions on the best way to set this up so I can start learning the correct tools to achieve my goal?
Thank you so much
PS: I did have a thought that maybe it needs to be driven by a switch that cuts power to the arduino and so when the switch is on the arduino switches on and all my requirements are in the setup section and the loop does nothing, then when the power is cutoff by the switch everything 'resets' BUT is this a healthy way to run an arduino?
What you want to do sounds quite possible. Your program needs to be in one of several states which to me seems to infer that implementing a state machine would be a good idea. Wait in a state until the required time passes or user input is received and then move to a new state and wait again and so on.
A state machine sounds interesting - I will start googling and look into it - thank you.
The problem with posting a program is that I have about 15 different ones and none of which are remotely close to working so it would only limit the possible solutions because people will try to solve the program versus the actual application - hope that makes sense.
I've tried using a while command, if else, delays, I have even tried using multiple loops and jumping back and forth but the problem is that I know too little about any one of them to execute them 100% which is why I have asked for some direction so I can spend enough time on something I know is going to work before moving on, rather than trying 10% of 50 different options and not knowing which one to put some serious time into understanding.
What you need is a state machine. The fist step should be designing a state diagram like the attached one. Once that is finished you can use my State machine library to convert the diagram to code. As you can see the states translate to functions 1:1 and the code shouldnt be hard to follow
#include <SM.h>
const byte iswitch = 2;//input pin for switch
const byte motor = 3;//output pin for motor
const byte plunger = 4;//output pin for plunger
const byte led = 13;//output pin for led
unsigned long retentive;//retentive timer variable
SM control(Ready);
void setup(){
pinMode(motor, OUTPUT);
pinMode(plunger, OUTPUT);
pinMode(led, OUTPUT);
}//setup()
void loop(){
EXEC(control);//run statemachine
}//loop()
State Ready(){
if(digitalRead(iswitch)){
digitalWrite(led, 1);//turn led on)
digitalWrite(motor, 1);//turn motor on
control.Set(ledOn);//change state
}//if(isitch)
}//Ready()
State ledOn(){
if(control.Timeout(300)){
retentive += 300;//update retentive timer
control.Set(ledOff);//change state
digitalWrite(led, 0);//turn led off
}//if(300ms)
if(retentive+control.Statetime()>=2000){//2s passed since first entry
retentive = 0;//reset timer
control.Set(plungerOn);//change state
digitalWrite(plunger, 1);//activate plunger
digitalWrite(led, 0);//turn led off
digitalWrite(motor, 0);
}//if(2s)
}//ledOn()
State ledOff(){
if(control.Timeout(300)){
retentive += 300;//update retentive timer
control.Set(ledOn);//change state
digitalWrite(led, 1);//turn led on
}//if(300ms)
}//ledOff()
State plungerOn(){
if(digitalRead(iswitch)){
digitalWrite(led, 0);//turn led off
digitalWrite(plunger, 0);//deactivate plunger
control.Set(wait);
}//if(switch pressed)
}//plunger()
State wait(){
if(!digitalRead(iswitch)) control.Set(Ready);//wait for switch to be released
}//wait()
The important thing is drawing a state diagram that models the sequence of events accurately. Once that is done you can write he code in any way you want. States are normally associated with output signals and transitions are associated with input or timer events.
The benefit of using my library is that you have a 1:1 relationship between states in the diagram and state functions that makes the code both easy to read and to debug if necessasy (seldom needed if you work this way) An other benefit is that timing is built in. A third is that you can run concurrent state machines and even arrays of them.
The most incredible and rewarding part of doing something like this - other than the amazing community and willingness from others to help of course
...is when you see an example sketch like the one posted by Nilton61, at first it is so confusing it muddles my brain completely, after spending about an hour reading up and understanding what exactly a state machine is trying to do and then going back through the sketch step by step it is actually amazing in it's simplicity once you understand it - I'm excited to get back into this again after a few weeks of frustrated dead ends
Robin2, that thread is really useful and actually exactly how I came to realise I needed some advanced guidance. I went through it a few weeks ago and got really close to sorting it out but I think I just lacked the knowledge of advanced solutions to apply to the problem at hand. It's difficult to research a topic when you don't know what the topic is
For some reasons some people shy away from state machines. I really do not understand why, because they are, as you state "amazing in their simplicity"