Introduction
This bed lamp is using a tilt sensor to control the colour of the light. Without pressing any button, just tilt the lamp until you get your favourite colour.
Video
This video will show you how to make a colourful bed lamp using tilt sensor on Maker UNO.
Hardware Preparation
This is the components needed.
Sample Code
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); | |
} | |
} |
References :
Thank you
Thank you for reading this tutorial and we hope it helps your project development. If you have any technical inquiry, please post at Cytron Technical Forum.