Pages: [1] 2   Go Down
Author Topic: Help with Arduino program  (Read 1381 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11718
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you tried the various examples that control LEDs?  Its not clear exactly what you want to do?
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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            (Starts at 2:00)
Logged

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11718
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So you just need to rewrite that paragraph in C really... 
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Code:
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
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
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
  
  
}
« Last Edit: September 19, 2012, 03:04:15 pm by amo0795 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-razz
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Pages: [1] 2   Go Up
Jump to: