Mikedmor.com - Stepping stones
Mikedmor Blog Blog Blog Stepping stones
Posted on Jan 12th, 2012 at 0 Comments

So for my first project i began to just build something that would allow me to toggle the colors through the serial port built into the arduino program (you can get that from this site). To keep it simple
r or R toggled red
g or G toggled green
b or B toggled blue
W***! for adding a pause (just replace *** with any number, in milliseconds)
and o or O toggled all of them off.

From that point i moved on to adding a few buttons. The purpose of these buttons were for starting a recording session, playing back a recording, and toggling the different colors. So there was a total of 5 buttons. You can find all the wiring in the main image if you need help. I apologize for the wires, i'm not very neat and tidy but also these smaller projects will never stay build for more than a week.

Last but not least here is the code you will need in order to make all this work. Please remember that i am extremely new to this so my code may not be the best. Below the code is a change log for anyone that is interested. I will update this post as i fix and update the code.

//**********Written By: Mikedmor**********//
//************January 5th 2012************//


//This is a great serial program that will allow any program to interact with the multicolor led
//on my current arduino setup. The setup is simple and described below if your wanting to recreate
//it. My favorite string to input in the serial is also below.
//


const int RLED=9;
const int GLED=10;
const int BLED=11;

//*Experimental*//
const int bLED=8;
const int PLAY=2;
const int BIND=3;
const int RBUT=4;
const int GBUT=5;
const int BBUT=6;

byte inByte; //initialized instantly in loop
int pause = 0; //wait for wait command

//*Experimental*//
int LEDState=HIGH;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 80;    // the debounce time; increase if the output flickers
int is_bound=0;
char str[550]="";
int strloc=0;
int rstate=HIGH;
int gstate=HIGH;
int bstate=HIGH;
int aserial=0;
int aserloc=0;

void setup(){
  //*Experimental*//
  pinMode(bLED, OUTPUT);
  pinMode(PLAY, INPUT);
  pinMode(BIND, INPUT);
  pinMode(RBUT, INPUT);
  pinMode(GBUT, INPUT);
  pinMode(BBUT, INPUT);

  pinMode(RLED, OUTPUT);
  pinMode(GLED, OUTPUT);
  pinMode(BLED, OUTPUT);
  //setup pins on high (needed for RGB led)
  digitalWrite(RLED, HIGH);
  digitalWrite(GLED, HIGH);
  digitalWrite(BLED, HIGH);
  //Start the serial port
  Serial.begin(9600);
}

void loop(){
  if(Serial.available() > 0 || aserloc != 0){ //check if there is data waiting in the serial
    if(aserloc != 0){
      inByte = str[aserial-aserloc];
      aserloc--;
    }else{
      inByte = Serial.read();
    }
    if(inByte == 'R' || inByte == 'r'){  //Command for RED
      if(rstate==HIGH){
        rstate=LOW;
      }else{
        rstate=HIGH;
      }
      Serial.println("Toggling Red.");
    }
    else if(inByte == 'G' || inByte == 'g'){  //Command for GREEN
      if(gstate==HIGH){
        gstate=LOW;
      }else{
        gstate=HIGH;
      }
      Serial.println("Toggling Green.");
    }
    else if(inByte == 'B'|| inByte == 'b'){  //Command for BLUE
      if(bstate==HIGH){
        bstate=LOW;
      }else{
        bstate=HIGH;
      }
      Serial.println("Toggling Blue.");
    }
    else if(inByte == 'O'|| inByte == 'o'){  //Command for OFF
      rstate = HIGH;
      gstate = HIGH;
      bstate = HIGH;
      Serial.println("Turning off all.");
    }
    else if(inByte == 'W'|| inByte == 'w'){  //Command for WAIT
      pause=0;                                //Sets pause to 0 for each time you see a W
      if(aserloc != 0){                      //preload next buffer value
        inByte = str[aserial-aserloc];
        aserloc--;
      }else{
        inByte = Serial.read();
      }                 
      while(inByte != '!'){                   //to end the wait ! needs to be at the end
        if((inByte-'0') < 10)                 //Note: This was the biggest pain, for some reason the buffer
          //     has many junk values (255 being one). So i have it check
          //     for a value that is below 10 which it should be anyway 
          //     (its only a byte!). also in order to get the correct int
          //     value from a byte you must subtract '0'.
        {
          pause = pause*10;                   //Muliply last pause by 10 to make it easier
          pause = pause+(inByte-'0');         //Add new inByte as new ones, last is now the tens place.
        }
        if(aserloc != 0){                     //buffer for the next read(will stop if ! is read)
          inByte = str[aserial-aserloc];
          aserloc--;
        }else{
          inByte = Serial.read();
        }
      }
      Serial.print("Waiting for ");
      Serial.print(pause);
      Serial.println(" miliseconds.");
      delay(pause);
      Serial.println("Done Waiting");
    }
    else{
      Serial.println("Invalid Command... Skipping");
    }
  }
  
  
  digitalWrite(RLED, rstate);
  digitalWrite(GLED, gstate);
  digitalWrite(BLED, bstate);
  
  //This needs to be set to the BIND key!
  int binding = digitalRead(BIND);
  int playing = digitalRead(PLAY);
  int ron     = digitalRead(RBUT);
  int gon     = digitalRead(GBUT);
  int bon     = digitalRead(BBUT);
  
  //Simple button toggles (nothing to see here!)
  if(ron == HIGH && lastButtonState == LOW && millis()-lastDebounceTime > debounceDelay){
    Serial.println("Manual Red toggle.");
    if(rstate == HIGH){
      rstate = LOW;
    }
    else{
      rstate = HIGH;
    } 
  }
  if(gon == HIGH && lastButtonState == LOW && millis()-lastDebounceTime > debounceDelay){
    Serial.println("Manual Green toggle.");
    if(gstate == HIGH){
      gstate = LOW;
    }
    else{
      gstate = HIGH;
    } 
  }
  if(bon == HIGH && lastButtonState == LOW && millis()-lastDebounceTime > debounceDelay){
    Serial.println("Manual Blue toggle.");
    if(bstate == HIGH){
      bstate = LOW;
    }
    else{
      bstate = HIGH;
    } 
  }
  
  //Play button
  if(playing == HIGH && lastButtonState == LOW && millis()-lastDebounceTime > debounceDelay){
    Serial.println("\nPlaying:");
    Serial.println(str);
    aserial=strlen(str);
    aserloc=strlen(str);
    lastDebounceTime = millis();
    if(aserial==0){
      Serial.println("Nothing to play!");
    }else{
      rstate = HIGH;
      gstate = HIGH;
      bstate = HIGH;
    }
  }

  //Toggle to enter while loop
  if(binding == HIGH && lastButtonState == LOW && millis()-lastDebounceTime > debounceDelay){
    Serial.println("Start Recording:");
    char str[550]="";
    is_bound = 1;
    lastDebounceTime = millis();
  }
  
  //Changes button state if any of the buttons are presses
  if(binding == HIGH || playing == HIGH || ron == HIGH || gon == HIGH || bon == HIGH){
      lastButtonState = HIGH;
    }
    else{
      lastButtonState = LOW;
    }
    
  strloc=0;
  
  //Loop for remembering keys
  while(is_bound == 1){
    if(Serial.available() > 0){
      Serial.println("Cannot use Serial while binding is on!");
      Serial.println("Exiting binding!");
      is_bound = 0;
    }
    binding = digitalRead(BIND);
    playing = digitalRead(PLAY);
    ron     = digitalRead(RBUT);
    gon     = digitalRead(GBUT);
    bon     = digitalRead(BBUT);
    if(binding == HIGH && lastButtonState == LOW && millis()-lastDebounceTime > debounceDelay){
      Serial.println("Recording Over!\nSaving...");
      is_bound = 0;
      rstate = HIGH;
      gstate = HIGH;
      bstate = HIGH;
      lastDebounceTime = millis();
      Serial.println("Done Saving!");
    }

    //Red Toggle and wait add
    if(ron == HIGH && lastButtonState == LOW && millis()-lastDebounceTime > debounceDelay){
      str[strloc] = 'W';
      int wait=millis()-lastDebounceTime;
      char bwait[8]="";
      for(int i=0; wait != 0 && i < 8; i++){
        bwait[i]= (wait%10)+'0';
        wait = wait/10;
      }
      for(int i=(strlen(bwait)-1);i>-1;i--){
        strloc++;
        str[strloc] = bwait[i];
      }
      strloc++;
      str[strloc] = '!';
      strloc++;
      //end of 1000 millisecond default for now
      str[strloc] = 'R';
      strloc++;
      if(rstate == HIGH){
        rstate = LOW;
      }
      else{
        rstate = HIGH;
      } 
      Serial.println("Toggle RED");
      lastDebounceTime = millis(); 
    }
    //Green Toggle and wait add
    if(gon == HIGH && lastButtonState == LOW && millis()-lastDebounceTime > debounceDelay){
      str[strloc] = 'W';
      int wait=millis()-lastDebounceTime;
      char bwait[8]="";
      for(int i=0; wait != 0 && i < 8; i++){
        bwait[i]= (wait%10)+'0';
        wait = wait/10;
      }
      for(int i=(strlen(bwait)-1);i>-1;i--){
        strloc++;
        str[strloc] = bwait[i];
      }
      strloc++;
      str[strloc] = '!';
      strloc++;
      //end of 1000 millisecond default for now
      str[strloc] = 'G';
      strloc++;
      if(gstate == HIGH){
        gstate = LOW;
      }
      else{
        gstate = HIGH;
      } 
      Serial.println("Toggle GREEN");
      lastDebounceTime = millis(); 
    }
    //Blue Toggle and wait add
    if(bon == HIGH && lastButtonState == LOW && millis()-lastDebounceTime > debounceDelay){
      str[strloc] = 'W';
      int wait=millis()-lastDebounceTime;
      char bwait[8]="";
      for(int i=0; wait != 0 && i < 8; i++){
        bwait[i]= (wait%10)+'0';
        wait = wait/10;
      }
      for(int i=(strlen(bwait)-1);i>-1;i--){
        strloc++;
        str[strloc] = bwait[i];
      }
      strloc++;
      str[strloc] = '!';
      strloc++;
      //end of 1000 millisecond default for now
      str[strloc] = 'B';
      strloc++;
      if(bstate == HIGH){
        bstate = LOW;
      }
      else{
        bstate = HIGH;
      } 
      Serial.println("Toggle BLUE");
      lastDebounceTime = millis(); 
    }


    //Changes button state if any of the buttons are presses
    if(binding == HIGH || playing == HIGH || ron == HIGH || gon == HIGH || bon == HIGH){
      lastButtonState = HIGH;
    }
    else{
      lastButtonState = LOW;
    }
    
    if(strloc > 500 || millis()-lastDebounceTime > 86400000){
      Serial.println("Ran out of space for remember commands!");
      Serial.println("Exiting loop!");
      is_bound = 0;
      rstate = HIGH;
      gstate = HIGH;
      bstate = HIGH;
    }

    //Changes LED based on if the loop will continue or its state
    digitalWrite(bLED, is_bound);
    digitalWrite(RLED, rstate);
    digitalWrite(GLED, gstate);
    digitalWrite(BLED, bstate);
  }
}



Change Log
//****Version 0.1****//
//------Features-------//
//Serial Controller
//Button Controller
//6 different colors
//Memorization of small sequences
//
//------Bugs-------//
//Locks up on large inputs

0 Comments to “Stepping stones”

Leave a Reply

  • +Michael Morris
  • Mikedmor Youtube
  • Mikedmor Linkedin
  • Mikedmor Facebook
  • Mikedmor Twitter
  • Mikedmor RSS

© 2010 Copyright Mikedmor. All Rights Reserved.