Tuesday 8 December 2009

Arduino - code - mood lamp

byte ledRed =9;
byte ledGrn = 10;
byte ledBlu = 11;
byte ledReady = 13;
byte button[] = {2, 3, 4};
int buttonstate[]= {0,0,0};
int buttonlevel[]= {0,0,0};
int ledval[] = {0,50,100,150,200,255};
unsigned long lastpressed[] = {0,0,0};

void setup(){
 
  pinMode (ledReady,OUTPUT); // I use this to notify me when reset completed
  pinMode (ledRed,OUTPUT);
  pinMode (ledGrn, OUTPUT);
  pinMode (ledBlu, OUTPUT);
  for (int x =0;x<=3; x++)
  {
  pinMode (button[x], INPUT); // configure buttons
  }
  digitalWrite (ledRed, LOW);
  digitalWrite (ledGrn, LOW);
  digitalWrite (ledBlu, LOW);
  digitalWrite (ledReady,HIGH); // I use this to notify me when reset completed
  /* Serial.begin(9600); // for debugging only */
}

void loop(){
 

for (int x = 0;x <=2; x++){

buttonstate[x] = digitalRead (button[x]); // check if 1 of 3 buttons pressed
 
if (buttonstate[x] == HIGH  && (millis()- lastpressed[x]) >500) {
 buttonlevel[x] +=1;
if (buttonlevel[x] > 5) {buttonlevel[x] = 0;}

 /* Serial.println ( buttonlevel[x]); // for debug */
lastpressed[x] = millis(); // get time of when button pressed used for debounce

}
}
  analogWrite (ledRed, ledval[buttonlevel[0]]); // set level of Red LED
  analogWrite (ledGrn, ledval[buttonlevel[1]]); // set level of Green LED
  analogWrite (ledBlu, ledval[buttonlevel[2]]); // set level of Blue LED */
   delay (250);
 }

Sunday 6 December 2009

Arduino mood lamp

Built another Arduino prototype of mood lamp controlled by 3 button to change individual light levels of RGB leds, next version will be automatically controlled.

Arduino - code - Happy Birthday Melody with flickering candle

/*  Happy Birthday Melody with flickering Candle
 *  By Chris Hawkins  November 2009
 *  with modified code from the  Melody example
 * (cleft) 2005 D. Cuartielles for K3
 *
 *
 * Taken from (cleft) 2005 D. Cuartielles for K3
 *
 * The calculation of the tones is made following the mathematical
 * operation:
 *
 *       timeHigh = period / 2 = 1 / (2 * toneFrequency)
 *
 * where the different tones are described as in the table:
 *
 * note  frequency  period  timeHigh
 * c 1         261 Hz          3830  1915  
 * d 2         294 Hz          3400  1700  
 * e 3         329 Hz          3038  1519  
 * f 4         349 Hz          2864  1432  
 * g 5         392 Hz          2550  1275  
 * a 6         440 Hz          2272  1136  
 * b 7         493 Hz          2028 1014 
 * C 8         523 Hz         1912  956
 *
 * http://www.arduino.cc/en/Tutorial/Melody
 *
 *
 */ 
int speaker = 6;
int led1pin = 9; // RED LED
int led2pin = 10; // Yellow LED
int button =4; // state swicth to play music
int x = 0;
int buttonState = 0;
int length = 28; // the number of notes
char notes[] = "ccdcfe ccdcgf ccCafed bbafgf"; // a space represents a rest
int beats[] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1, 1,1,1,1,1,1,1,1,1,1,1,1,2};
int tempo = 300; 
void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speaker, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speaker, LOW);
    delayMicroseconds(tone);
  }

void playNote(char note, int duration) {
  char names[] = {
    'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'   };
  int tones[] = {
    1915, 1700, 1519, 1432, 1275, 1136, 1014, 956   }; 
  // play the tone corresponding to the note name
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }

void playmusic ()
{
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    }
    else {
      if (i % 2 == 0)
      {
        analogWrite (led1pin,200);
       analogWrite (led2pin,100);
      }
      else {
        analogWrite (led1pin,100);
        analogWrite (led2pin,200);
      }
      playNote(notes[i], beats[i] * tempo);
     
    } 
    // pause between notes
    delay(tempo / 2);
  }
  digitalWrite (led1pin,LOW);
  digitalWrite (led2pin,LOW);

void setup() {
  pinMode(speaker, OUTPUT); /* Speaker to play tune */
  pinMode(led1pin, OUTPUT); /* Red led from tri colour LED */
  pinMode(led2pin, OUTPUT); /* Yellow led from tri colour LED */
  pinMode (button, INPUT);  /* Start playing melody button */

void loop(){
  buttonState = digitalRead (button); /* check if button pressed */
  if (buttonState == HIGH){
    playmusic();
  }
}

Saturday 5 December 2009

Arduino Happy Birthday Melody with flickering candle

Here is my Arduino playing Happy Birthday that I coded and designed for My Boys birthday http://www.youtube.com/watch?v=PNmP1wJUhgc

Christmas Tree 2009

Been to B&Q today with family to get our Christmas Tree and now it's fully decorated it looks lovely.

Hello world!

Welcome to Wordpress.com. This is your first post. Edit or delete it and start blogging!