Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This sample code is for Colourful Bed Lamp Using Tilt Sensor on Maker UNO tutorial.
const int tiltPin = 2;
const int redLed = 11;
const int greenLed = 10;
const int blueLed = 9;
int tiltState;
int count = 0;
void setup() {
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);// initialize the pushbutton pin as an input:
pinMode(blueLed, OUTPUT);
pinMode(tiltPin, INPUT);
}
void loop() {
tiltState = digitalRead(tiltPin);
if (tiltState == HIGH) {
count++;
if (count > 8) {
count = 0;
}
switch (count) {
case 0: // off
analogWrite(redLed, LOW);
analogWrite(greenLed, LOW);
analogWrite(blueLed, LOW);
break;
case 1: //red
analogWrite(redLed, 255);
analogWrite(greenLed, 0);
analogWrite(blueLed, 0);
break;
case 2: //orange
analogWrite(redLed, 255);
analogWrite(greenLed, 128);
analogWrite(blueLed, 0);
break;
case 3: //yellow
analogWrite(redLed, 255);
analogWrite(greenLed, 255);
analogWrite(blueLed, 0);
break;
case 4: //green
analogWrite(redLed, 0);
analogWrite(greenLed, 255);
analogWrite(blueLed, 0);
break;
case 5: //blue
analogWrite(redLed, 0);
analogWrite(greenLed, 0);
analogWrite(blueLed, 255);
break;
case 6: //purple
analogWrite(redLed, 127);
analogWrite(greenLed, 0);
analogWrite(blueLed, 255);
break;
case 7: //pink
analogWrite(redLed, 255);
analogWrite(greenLed, 0);
analogWrite(blueLed, 255);
break;
case 8: //white
analogWrite(redLed, 255);
analogWrite(greenLed, 255);
analogWrite(blueLed, 255);
break;
default:
break;
}
delay(500);
while (digitalRead(tiltPin));
delay(500);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment