Colourful Bed Lamp Using Tilt Sensor on Maker UNO

Let's try to make a colourful🌈 bed lamp. Tilt it to get your favourite colour 😎

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);
}
}
view raw bedLamp.ino hosted with ❤ by GitHub

References :

  1. Displaying on MAX7219 Dot Matrix Using Arduino

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.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

Smart Door Announcer with Raspberry Pi
Why Raspberry Pi is the future of computing devices?
Face Recognition Door Lock System Using OpenCV on Raspberry Pi
Send Data to Firebase Using Arduino Uno WiFi Rev2
Simple Alarm System Using Maker Nano
Scroll to Top