Im new to the forums and arduino but im trying to make a series of 6 leds that can be programmed for 2 modes using a pushbutton. Could you help me out with the program and diagram that you think would work? Anyone's help is greatly appriciated, thanks.
Have you tried the various examples that control LEDs? Its not clear exactly what you want to do?
Hey Mark, basically i need there to be 6 leds that with a power switch and a mode button(i will eventually have it on some perf board). When first switched on i need it to blink a few times to say its working, then for mode 1 i need to it pause 4hours and then start a blinking sequence. For mode 2 i need it to start the blinking sequence after 5 minutes. For the blinking sequence i need to have two sides with 3 leds and have the inner two leds blink then the middle ones and finally the two on the outside and have that pattern repeated for 10 seconds. That 10 sec pattern should go off every 15 mins. Here is a link to the product i am trying to mimic so you can understand more. Thanks - YouTube (Starts at 2:00)
So you just need to rewrite that paragraph in C really...
Ya basically lol. I used the search function a little and found some stuff but basically the two questions Im having trouble with are what functions to use for switching modes such as Click once for mode 1 and twice for mode 2. Also ive read that when you take the atmega328 out of the arduino and put it into a circut, you need a crystal oscillator. Is that needed if I plan on putting it on a perf board. Once I can get help with the mode ill see what code I can put together and show you
what functions to use for switching modes such as Click once for mode 1 and twice for mode 2.
There isn't one function. There are sever things you need to do. First, you need to detect when the switch transitions from released to pressed. To do this, you need to read the current state of the switch, and compare that to the previous state of the switch. If not the same, a transition occurred. That transition could be from pressed to released or from released to pressed. The current state (pressed or released) tells you which way the transition went.
This implies, of course, that you need to save the current state of the switch, at the end of loop(), as the previous state for the next pass.
When a transition occurs, and that transition is from released to pressed, increment a counter. Base the rest of the code on the value of the counter. Reset the counter when it exceeds the number of modes you have code for.
Also ive read that when you take the atmega328 out of the arduino and put it into a circut, you need a crystal oscillator. Is that needed if I plan on putting it on a perf board.
Yes. You may want to consider using a Mini, Pro Mini, or Nano instead, to save the need to develop your own PCB/design.
Im kind of confused about the counter. I have some knowledge of programming but could i make it an if,else statement where IF the button is pressed, it does mode 2 and if it isnt pressed then do mode 1? Sorry if that didnt make sense but im not that familiard with C functions. And i was planning on getting an Arduino Uno r3 because ive seen a lot of projects using it and it seems like a good place to start. Thanks
but could i make it an if,else statement where IF the button is pressed, it does mode 2 and if it isnt pressed then do mode 1?
Sure, if that's what you want.
Making the mode change each time the switch is pressed is not that difficult, though.
int currState;
int prevState = HIGH; // Whatever not pressed means, for your wiring
int mode = 0;
void loop()
{
currState = digitalRead(somePin);
if(currState != prevState)
{
if(currState == LOW) // Or whatever pressed means, for your wiring
mode++;
}
prevState = currState;
if(mode > x)
mode = 0;
if(mode == 0)
// do one thing
else if(mode == 1)
// do something else
else if(mode == 2)
// Do whatever
}
I know you already posted code but i had this done before i saw that. Check this out and see if everything looks logical. Thanks
void setup();
{
pinMode(13, OUTPUT); //outer two leds
pinMode(12, OUTPUT); //middle two leds
pinMode(7, OUTPUT); //inner two leds
pinMode(8, INPUT); //switch
digitalWrite(13, HIGH);// blink LEDs once to signal power on
digitalWrite(12, HIGH);
digitalWrite(7, HIGH);
delay(2000);
digitalWrite(13, HIGH);// blink LEDs once to signal power on
digitalWrite(12, HIGH);
digitalWrite(7, HIGH);
delay(2000);
}
if(8=HIGH) //if switch is on (mode 1)
{
delay(14400000); // Wait 4 hours for sleep to start
}
else // is switch is off(mode 2)
{
delay(300000); //Wait 5 mins for sleep to start for nap
}
void loop() //do blinking after delay
{
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW); //Blink outer two leds that are connected to the same output
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
digitalWrite(12, HIGH);
delay(500);
digitalWrite(12, LOW); //Blink middle two leds that are connected to the same output
delay(500);
digitalWrite(12, HIGH);
delay(500);
digitalWrite(12, LOW);
delay(500);
digitalWrite(7, HIGH);
delay(500);
digitalWrite(7, LOW); //Blink inner two leds that are connected to the same output
delay(500);
digitalWrite(7, HIGH);
delay(500);
digitalWrite(7, LOW);
delay(500);
delay(600000); //Wait 10 minutes
}
And you compiled this? I didn't think so...
There are a number of flaws in that code that will not allow it to compile.
PaulS's code sample is good, although you should put his code in a function and call it once every few tens of ms, otherwise the mode change will be so fast you'll end up with a random selection.
Also, see "blink without delay" example to get rid of those nasty delay()s
otherwise the mode change will be so fast you'll end up with a random selection.
In my code, the mode changes only once each time the switch is pressed. The speed at which it changes depends entirely on how fast the user presses and releases the switch.
PaulS:
otherwise the mode change will be so fast you'll end up with a random selection.
In my code, the mode changes only once each time the switch is pressed. The speed at which it changes depends entirely on how fast the user presses and releases the switch.
Ouch! Your're right. There's the debouncing problem though... Without delay (or timed polling) the digitalRead() call will detect state changes while the button switch bounces.
There's the debouncing problem though...
True, but what OP wanted to do in each mode was going to take time, so debouncing would be handled by that.
PaulS:
There's the debouncing problem though...
True, but what OP wanted to do in each mode was going to take time, so debouncing would be handled by that.
Yes, but I was thinking about having the led pattern implemented with a state machine, rather than using delay().
Anyway your code was a very good starting point. Let's see what the OP thinks about it.
Yes, i think the code from PaulS looks really good with the modes and everything but the code i wrote was just something to show you what i wanted. Im not sure what is wrong with my code but it would help if someone could point that out. If i only want mode 1 and 2, how would i loop back to mode 1 if the button was pressed 3 times? Also, tuxduino, that sounds smart but i looked it up and i'm still not sure what a state machine does other that lets the program do other stuff during delays. Thanks
i'm still not sure what a state machine does
Try drawing a picture, then. Draw a circle. In the circle, write the name of the state (buttonPressedOnce, buttonPressedTwice, etc.). Then, draw arrows between the circles. Above the arrow, write the action that causes a change of state (switch pressed, for example). Below the arrow, write what needs to be accomplished during the transition.
If you think about this, you could have sub states (lightsOn and lightsOff). Some states can only be reached when another state is current. So, while the state is buttonPressedTwice, the trigger to transition from one state to another can be that a certain amount of time has passed. After a while, you transition from lightsOff to lightsOn or from lightsOn to lightsOff.
Certain actions occur when the transition happens - the last time a transition occurred might be recorded. This is useful for measuring how long it has been since the last transition, which is the criteria for transitioning again.
i looked it up and i'm still not sure what a state machine does other that lets the program do other stuff during delays.
Please carefully study the blink without delay example.
When you get rid of delay() calls, your code doesn't stop waiting for some time to pass, and runs at full speed. Therefore can't rely anymore on wall clock time between a line of code and the next to delay execution.
You have to ask yourself whether it's time to call a particular function or not. Most of the time it won't, but as soon as that particular event you've been waiting for happens, the test succeeds and you call that function.
In the blink example, you have two approaches:
turn led on
wait
turn led off
wait
repeat
how long ago did I change the led status variable ?
if more than 1000ms passed since then, then
invert the led status variable (e.g. if it's false make it true and viceversa)
if led status variable == true, turn led on, else turn led off
discard previous time mark and set it to current time (i.e. millis() value)
endif
// here I could put other timed actions
repeat
The state-based code is more complex, but you can repeat the structure many times and have different state variables (i.e. finite state machines), each controlled by its own logic, reading a certain set of input and acting on some output peripheral (pin, lcd, pwm, ecc.)
That sounds much better than the original code. Ill go type up some code and see what happens
I've rewritten from scratch my response three or four times... the "state" concept is obvious to me but I remember it was not easy to grasp at first, and definitely not easy to explain, I hope I was clear enough in my attempt.