LED’s

 

I used a piezo resistor to control the color of an RGB LED.

CODE:

int knockSensor = 0;

byte val = 0;

int THRESHOLD = 5;

int redPin = 12; // Red LED, connected to digital pin 9
int greenPin =11; // Green LED, connected to digital pin 10
int bluePin =10; // Blue LED, connected to digital pin 11

void setup() {

Serial.begin(9600);
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

}
void loop() {

val = analogRead(knockSensor);

if (val >= THRESHOLD) {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);

Serial.println(“Knock!”);

}

else{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}

delay(50
);

}

i also made this:

 

//code for fade taken and modified from http://www.instructables.com/id/Fading-RGB-LED-Arduino/?ALLSTEPS
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status
int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
int redVal = 255; // Variables to store the values to send to the pins
int greenVal = 1; // Initial values are Red full, Green and Blue off
int blueVal = 1;

int i = 0; // Loop counter
int wait = 1; // 50ms (.05 second) delay; shorten for faster fades
void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600); // …set up the serial ouput on 0004 style
}
}
#define fade(x,y) if (x>y) x–; else if (x<y) x++;

void loop() {

buttonState = digitalRead(buttonPin);
if (buttonState == HIGH){
action();
}
else{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
}
void action(){

{
i += 1; // Increment counter
if (i < 255) // First phase of fades
{
redVal -= 1; // Red down
greenVal += 1; // Green up
blueVal = 1; // Blue low
}
else if (i < 509) // Second phase of fades
{
redVal = 1; // Red low
greenVal -= 1; // Green down
blueVal += 1; // Blue up
}
else if (i < 763) // Third phase of fades
{
redVal += 1; // Red up
greenVal = 1; // Green low
blueVal -= 1; // Blue down
}
else
{
i = 1;
}

analogWrite(redPin, redVal); // Write current values to LED pins
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
}
delay(wait); // Pause for ‘wait’ milliseconds before resuming the loop
}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *