Go Down

Topic: Help- newbie here, motor project 1 button 3 modes (Read 814 times) previous topic - next topic


Feb 05, 2013, 08:22 pm Last Edit: Feb 05, 2013, 08:40 pm by joejet Reason: 1
Hi, if anyone has 5 mins please can you help me,

I am helping my son on a school project we are making a game for an Easter fair and i need some pointers on programming the arduino.
I have an arduino setup with a button input (10) and an H-bridge on 2 output pins (11,12) controlling a std dc motor i also am using pin 13 as an LED

this is what i want to do:-

push the button and run the motor for 1000ms (advance), I also want to flash the led during the motor running,

Push the button again and now run the motor for 3000ms(rotate) and again flash the LED, it would be great to have a slower flash

finally if the button was initially pressed i need to reverse the motor for 1000ms (Reverse) but only if there is no other activity for 10000ms

I can write code to run the h-bridge to flash led's and do timing but i cant seem to tie it all together.

I need to use some sort of flag something like this:
Press1        Advance, set flag and flash led, start 10000 timer
Press2        only if flag set  Rotate flash led and reset 10000 timer leave flag set
if flag not set and counter expired reverse for 250ms then reset flag again flash led same speed at press 1

is this the correct sort of thinking?

how do i use flags? or whatever they are called

how can i flash the led's as I am using the delay(250) to run the motor the whole program pauses; is there a better way

Finally if a button is pressed during a motor run it must be ignored, and not cached.

Is this possible? and can anyone give me some pointers, I am so much better at hardware than software and I am regretting saying i could sort this. oops!!

thanks any help would be really appreciated.



how can i flash the led's as I am using the delay(250) to run the motor; is there a better way

The most important principle you need to follow is to design your sketch so that it does not need to block (i.e. stop and wait for something to happen).

The way to achieve this is to design your sketch so that the code in loop() checks for things that may need to be done, and does whatever may be needed. If something doesn't need to be done (yet), loop() continues checking the other things. Loop is called repeatedly, so anything that doesn't need to be done yet will be checked again shortly. For this to work, none of your code must take very long to complete, where 'very long' is relative to how often you need to check everything else.

The 'blink without delay' sketch demonstrates how to use this approach to perform actions on a timed basis but doesn't explain how fundamentally important it is that you you do this.

The use of a non-blocking design also impacts the structure of your control logic. In a blocking design, you can implement a sequence of actions and events by writing code that takes an action and waits for the expected event; where you are in the sequence of actions/events is implicit based on where you are in the code. This doesn't work when you use a non-blocking architecture. Instead, you have to use variables to record where you have got to in each sequence. Usually, this would be implemented as a state machine. For example, your description suggests a state machine with the following states:

Initial (waiting for the initial switch input)
Advancing (waiting for the 1000 ms elapsed time to pass)
Advanced (waiting for switch input to start the rotation)
Rotating (waiting for 3000 ms elapsed time to pass)
Rotated (waiting for 10000  ms elapsed time to pass before starting the reverse)
Reversing (waiting for 1000 ms elapsed time to pass)

At each step the code would need to check whether the required time has elapsed, or the expected switch input has arrived, or whether it is time to toggle the state of the LED to make it blink. Individually, each of these are very simple to code. When you bring them together into a state machine you can produce very complex and powerful behaviour.

An easy way to implement a state machine is to define a set of constants defining the states, a variable holding the current state, and use a switch statement to test for the events that are relevant to each state.


Thanks PeterH for that, I know of get it, it does seem more complicated than it sounded initially!

I can get each step working at least, so I'm not totally useless, well nearly

I will try and get it sorted, but it might have to wait till the weekend, work always gets busy when I bring a lunchtime project in!

If anyone has time to give a sample line of Code for the sections PeterH mentiond in his last paragraph, I would really appriciate it. (just a quick sample one line then I at least know what I should be using, and can look at other samples, thanks joejet

define a set of constants defining the states
a variable holding the current state,
And a switch statement to test for

Go Up