Wednesday, June 3, 2015

Arduino Programs

Arduino Mood Lamp 

Oscillating lights program:


/*
 Mood Lamp - oscillates between colors
 */ 
 
// each pin corresponds to an LED color:
int led0 = 9;
int led1 = 10;
int led2 = 11; 

//declare internal variables
int brightness = 100;
int red = 0;
int blue = 0;
int green = 0;
int x = 0;


// this routine runs each time you hit the reset button
void setup() {                
  // declare the relevant pins to be output
  pinMode(led0, OUTPUT);    
  pinMode(led1, OUTPUT);    
  pinMode(led2, OUTPUT);    
}
void loop() {
  // put your main code here, to run repeatedly:
  for (float x=0; x < PI; x = x + .000004){
    red = brightness * abs(sin(x*(180/PI)));
    green = brightness * abs(sin((x+PI/3)*(180/PI)));
    blue = brightness * abs(sin((x+(2*PI)/3)*(180/PI)));
    analogWrite(led0, red);
    analogWrite(led1, green);
    analogWrite(led2, blue);
  }
}




Fading Program:


int led0 = 11;
int brightness = 0;
int fadeAmount = 15 ;


void setup() {
  // put your setup code here, to run once:
  pinMode(led0,OUTPUT);
}

void loop() {
  // brings the brightness of the LED down slowly
  analogWrite(led0, brightness);
  brightness = brightness + fadeAmount;  
  if (brightness == 0 || brightness ==225){
     fadeAmount = -fadeAmount;
  }
 delay(1000); 
}



No comments:

Post a Comment