loading

Help for first Arduino project

Greetings all!

I just received my Arduino board in the mail on Saturday, and trying to learn how to write code for it.

I have figured out how to make a button press trigger a pattern of LED's.  When button1 is pressed, pattern 1 is played.  When button 2 is pressed, it will run pattern 2 (once the first pattern has run twice).

I would like to learn how to:

1.  Keep a pattern running continuously (unless the other pattern is initiated).
2.  Press the same button a second time, to turn the pattern off.

Here is my code (forgive the sloppiness...I'm new at this):

int pinled1 = 1;
int pinled2 = 2;
int pinled3 = 3;
int pinled4 = 4;
int button1 = 5;
int button2 = 6;
int buttonstate1 = 0;
int buttonstate2 = 0;


void setup()
{
  pinMode(pinled1, OUTPUT);
  pinMode(pinled2, OUTPUT);
  pinMode(pinled3, OUTPUT);
  pinMode(pinled4, OUTPUT);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(8,OUTPUT);
}

void loop()
{
  digitalWrite(8,HIGH);
  buttonstate1 = digitalRead(button1);
  buttonstate2 = digitalRead(button2);

if (buttonstate1 == HIGH){
{
  buttonstate1 = digitalRead(button1);
  buttonstate2 = digitalRead(button2);

    digitalWrite(pinled1,HIGH);
    digitalWrite(pinled2,HIGH);
    delay(500);
    digitalWrite(pinled1,LOW);
    digitalWrite(pinled2,LOW);
    digitalWrite(pinled3,HIGH);
    digitalWrite(pinled4,HIGH);
    delay(500);
    digitalWrite(pinled3,LOW);
    digitalWrite(pinled4,LOW);
   
}}

else if (buttonstate2 == HIGH){

{
  buttonstate1 = digitalRead(button1);
  buttonstate2 = digitalRead(button2);

    digitalWrite(pinled2,LOW);
    digitalWrite(pinled1,HIGH);
    delay(500);
    digitalWrite(pinled1,LOW);
    digitalWrite(pinled2,HIGH);
    delay(500);
    digitalWrite(pinled2,LOW);
    digitalWrite(pinled3,HIGH);
    delay(500);
    digitalWrite(pinled3,LOW);
    digitalWrite(pinled4,HIGH);
    delay(500);
    digitalWrite(pinled4,LOW);
    digitalWrite(pinled3,HIGH);
    delay(500);
    digitalWrite(pinled3,LOW);
    digitalWrite(pinled2,HIGH);
    delay(500);
    digitalWrite(pinled2,LOW);
}}

{
  buttonstate1 = digitalRead(button1);
  buttonstate2 = digitalRead(button2);
}}

Thank you to anyone who can pass on some knowledge!

sort by: active | newest | oldest
huck alexander (author) 5 years ago
Thank you very much mpilchfamily!

My code was written in several different windows, and I think when I combined them, I included too many brackets. Very good point on using notations. I will start doing that.

Using the void pattern1() and void pattern2() sections will make things much easier. I was not aware how to do that.

Your code runs well, but I still run into the issue with the pattern running twice, then stopping. I can not figure out why each pattern runs twice (as opposed to once).

Regardless, you gave me a new angle to approach this project with. Thank you kindly for your time!

Ok i've updated the if() statements in the pattern functions to be more specific so we know they are not the reason the patters are stopping prematurely. Hope this helps. I can also show you how to simplify a few things and tighten up your code with some for() loops.

int pinled1 = 1;
int pinled2 = 2;
int pinled3 = 3;
int pinled4 = 4;
int button1 = 5;
int button2 = 6;
int buttonstate1 = 0;
int buttonstate2 = 0;
int loops = 0;  //for tracking which pattern should be running

void setup()
{
  pinMode(pinled1, OUTPUT);
  pinMode(pinled2, OUTPUT);
  pinMode(pinled3, OUTPUT);
  pinMode(pinled4, OUTPUT);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(8,OUTPUT);
}

void pattern1()  //pattern for button 1
{
  buttonstate1 = digitalRead(button1);
  buttonstate2 = digitalRead(button2);

  if (buttonstate1 == HIGH && loops == 1)
  {  //if button 1 is pressed again the pattern stops
    loops = 0;  
  }

  if (buttonstate2 == HIGH && loops ==1)
  {  //if button 2 is press move to pattern 2
    loops = 2;
    pattern2();  
  }

   digitalWrite(pinled1,HIGH);
   digitalWrite(pinled2,HIGH);
   delay(500);
   digitalWrite(pinled1,LOW);
   digitalWrite(pinled2,LOW);
   digitalWrite(pinled3,HIGH);
   digitalWrite(pinled4,HIGH);
   delay(500);
   digitalWrite(pinled3,LOW);
   digitalWrite(pinled4,LOW);
}

void pattern2()  //pattern for button 2
{
  buttonstate1 = digitalRead(button1);
  buttonstate2 = digitalRead(button2);

  if (buttonstate2 == HIGH && loops == 2)
  {  //if button 2 is pressed again the pattern stops
    loops = 0;  
  }

  if (buttonstate1 == HIGH && loops == 2)
  {  //if button 1 is pressed move to pattern 1
    loops = 1;
    pattern1();  
  }

  digitalWrite(pinled2,LOW);
  digitalWrite(pinled1,HIGH);
  delay(500);
  digitalWrite(pinled1,LOW);
  digitalWrite(pinled2,HIGH);
  delay(500);
  digitalWrite(pinled2,LOW);
  digitalWrite(pinled3,HIGH);
  delay(500);
  digitalWrite(pinled3,LOW);
  digitalWrite(pinled4,HIGH);
  delay(500);
  digitalWrite(pinled4,LOW);
  digitalWrite(pinled3,HIGH);
  delay(500);
  digitalWrite(pinled3,LOW);
  digitalWrite(pinled2,HIGH);
  delay(500);
  digitalWrite(pinled2,LOW);
}


void loop()
{
  digitalWrite(8,HIGH);
  buttonstate1 = digitalRead(button1);
  buttonstate2 = digitalRead(button2);

  if (buttonstate1 == HIGH && loops == 0)
  {
    loops = 1;  
  }

  if (buttonstate2 == HIGH && loops == 0)
  {
    loops = 2;  
  }

  if (loops == 1)
  {  //as long as loops equals 1 pattern1 will run
    pattern1();
  }

  if (loops == 2)
  {  //as long as loops equals 2 pattern2 will run
    pattern2();
  }

}

 
huck alexander (author)  mpilchfamily5 years ago
Sent a reply, but apparently it never connected.

I noticed several things that brought questions from your RGB_colorwheel.

1. Boolean functions. Is boolean just an easier way of expressing a string of numbers? (thus potentially memory saving?)

2. Serial.begin. What is the purpose of designating a serial comm rate?

3. Analog pins. Though I have been reading some on the analog pins, I am not yet clear on how they are used, or the difference between them and digital pins. I understand analog v digital, but what differing purpose would the pins have?

The project I asked about is actually just a crucial piece of a larger project. I'll explain it quick. I am seeking to create a glove that had four led's. One at each finger tip. I would like to have a button on the side of each finger (momentary?) leaving the thumb bare. The idea is to have a SPDT switch, which will control the LED's in being either solid or display a pattern. In position1, each button would turn on and off a corresponding LED (once for on, twice for off). In position2, each button would display a pattern utilizing all four LED's. Each button would turn on and off a different pattern.

I have thought about doing this for some time, but never had the knowledge as to how. This glove would be used for lightpainting sessions. Lightpainting is basically a long exposure photograph, where the subject uses light sources in order to "paint" a picture. An example would be my profile picture. A result of lightpainting.

I did compile and run the code you suggested, but I ran into similar problems. The code would generally run each pattern twice per button press. Occasionally the pattern will only run once. I have read about "feedback resistors" used due to false readings on input pins when they are open. could this be the cause? I am using an arduino uno board. Also, I'm at a loss as to why the code would run each pattern twice instead of once (normally). Not that it is terribly important, but I'm doing this to learn, so if I can determine why, it will help me understand.

I appreciate you sharing your knowledge with me.

huck alexander (author)  huck alexander5 years ago
I took a quick break from trying to figure out the button situation, and dove in to trying to use my wii nunchuk with arduino. Ironically, though I couldn't figure out how to get an accurate read on the buttons, I was able to produce the desired end results a slightly different way. The joystick of the nunchuk is used to light leds individually (UP=Light pinled1, RIGHT=Light pinled2, etc..). When the "z button" is pressed along with joystick movement, each direction (U,D,R,L) triggers a pattern.

Here is the sketch:

#include
#include

#define BAUDRATE 19200

ArduinoNunchuk nunchuk = ArduinoNunchuk();

void setup()
{
Serial.begin(BAUDRATE);
nunchuk.init();
}
void loop()
{
nunchuk.update();
if(nunchuk.analogY > 200) { //joy up=pinled2 high
if (nunchuk.zButton == 1) { //if zbutton also pressed,pattern1
digitalWrite(5,HIGH);
digitalWrite(2,HIGH);
delay(500);
digitalWrite(5,LOW);
digitalWrite(2,LOW);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
delay(500);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
delay(500);
}
digitalWrite(2,HIGH);
}
else {
digitalWrite(2,LOW);
}
if(nunchuk.analogY < 50) { //joy down=pinled5,high
digitalWrite(5,HIGH); //if zbutton also pressed,pattern2
if (nunchuk.zButton == 1) {
digitalWrite(2,LOW);
digitalWrite(5,HIGH);
delay(500);
digitalWrite(5,LOW);
digitalWrite(2,HIGH);
delay(500);
digitalWrite(2,LOW);
digitalWrite(3,HIGH);
delay(500);
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
delay(500);
digitalWrite(4,LOW);
digitalWrite(3,HIGH);
delay(500);
digitalWrite(3,LOW);
digitalWrite(2,HIGH);
delay(500);
}
}
else {
digitalWrite(5,LOW);
}
if(nunchuk.analogX > 200) { //joy right=pinled3 high
if (nunchuk.zButton == 1) { //if zbutton also pressed,pattern3
digitalWrite(2,LOW);
digitalWrite(5,HIGH);
delay(500);
digitalWrite(5,LOW);
digitalWrite(2,HIGH);
delay(500);
digitalWrite(2,LOW);
digitalWrite(3,HIGH);
delay(500);
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
delay(500);
digitalWrite(4,LOW);
digitalWrite(3,HIGH);
delay(500);
digitalWrite(3,LOW);
digitalWrite(2,HIGH);
delay(500);
}
digitalWrite(3,HIGH);
}
else {
digitalWrite(3,LOW);
}
if(nunchuk.analogX < 50) { //joy left=pinled4 high
digitalWrite(4,HIGH);
if (nunchuk.zButton == 1) { //if zbutton also pressed,pattern4
digitalWrite(5,HIGH);
digitalWrite(2,HIGH);
digitalWrite(4,HIGH);
delay(500);
digitalWrite(2,LOW);
delay(500);
digitalWrite(3,HIGH);
delay(500);
digitalWrite(3,LOW);
delay(500);
}
}
else {
digitalWrite(4,LOW);
}
}


It is kind of cool to see the nunchuk control things, but ultimately, this would not be the desirable method to be used. One hand operation is helpful, especially if lightpainting by yourself.
Have a look at my 6 button RGB controller instructable. In there i have the code very well annotate so it should be easy to understand. It should teach you a few tricks you can use in your code such as using a for() loop to help tighten the code, reducing the amount of memory it will take up. 
I noticed you are using allot of unnecessary brackets in your code. You are using this:
if (buttonstate1 == HIGH){
{
When all you need is this:
if (buttonstate1 == HIGH)
{

Its also a good idea to get in the habit of annotating your code. Start making notes in your code so you know what is going on and others can understand it as well. As your codes grow and get more complex you will need notes in the code to help you keep track of what is happening in it.

Here is what i came up for you. This code is untested. I'm at work right now and just threw this together. It will compile but it hasn't been tested on an arduino yet. Fortunately i do have my arduino with me and can hook this up i just need to get to another PC where i can actually upload the code to my arduino. I'll try to test it a little later. But here is what i came up with.
 
int pinled1 = 1;
int pinled2 = 2;
int pinled3 = 3;
int pinled4 = 4;
int button1 = 5;
int button2 = 6;
int buttonstate1 = 0;
int buttonstate2 = 0;
int loops = 0;  //for tracking which pattern should be running

void setup()
{
  pinMode(pinled1, OUTPUT);
  pinMode(pinled2, OUTPUT);
  pinMode(pinled3, OUTPUT);
  pinMode(pinled4, OUTPUT);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(8,OUTPUT);
}

void pattern1()  //pattern for button 1
{
  buttonstate1 = digitalRead(button1);
  buttonstate2 = digitalRead(button2);
 
  if (buttonstate1 == HIGH)
  {  //if button 1 is pressed again the pattern stops
    loops = 0;   
  }
 
  if (buttonstate2 == HIGH)
  {  //if button 2 is press move to pattern 2
    loops = 2;
    pattern2();   
  }

   digitalWrite(pinled1,HIGH);
   digitalWrite(pinled2,HIGH);
   delay(500);
   digitalWrite(pinled1,LOW);
   digitalWrite(pinled2,LOW);
   digitalWrite(pinled3,HIGH);
   digitalWrite(pinled4,HIGH);
   delay(500);
   digitalWrite(pinled3,LOW);
   digitalWrite(pinled4,LOW);
}

void pattern2()  //pattern for button 2
{
  buttonstate1 = digitalRead(button1);
  buttonstate2 = digitalRead(button2);
 
  if (buttonstate2 == HIGH)
  {  //if button 2 is pressed again the pattern stops
    loops = 0;   
  }
 
  if (buttonstate1 == HIGH)
  {  //if button 1 is pressed move to pattern 1
    loops = 1;
    pattern1();   
  }

  digitalWrite(pinled2,LOW);
  digitalWrite(pinled1,HIGH);
  delay(500);
  digitalWrite(pinled1,LOW);
  digitalWrite(pinled2,HIGH);
  delay(500);
  digitalWrite(pinled2,LOW);
  digitalWrite(pinled3,HIGH);
  delay(500);
  digitalWrite(pinled3,LOW);
  digitalWrite(pinled4,HIGH);
  delay(500);
  digitalWrite(pinled4,LOW);
  digitalWrite(pinled3,HIGH);
  delay(500);
  digitalWrite(pinled3,LOW);
  digitalWrite(pinled2,HIGH);
  delay(500);
  digitalWrite(pinled2,LOW);
}
 

void loop()
{
  digitalWrite(8,HIGH);
  buttonstate1 = digitalRead(button1);
  buttonstate2 = digitalRead(button2);

  if (buttonstate1 == HIGH && loops == 0)
  {
    loops = 1;   
  }

  if (buttonstate2 == HIGH && loops == 0)
  {
    loops = 2;   
  }
 
  if (loops == 1)
  {  //as long as loops equals 1 pattern1 will run
    pattern1();
  }
 
  if (loops == 2)
  {  //as long as loops equals 2 pattern2 will run
    pattern2();
  }
 
}