Description
Get information from at least one analog sensor and at least one digital sensor (switch), and use this information to control at least two LEDs, one in a digital fashion and the other is an analog fashion, in some creative way.
Inspiration
LEDs usually go with a college dorm. Two good things usually happen in a college dorm – a sound sleep or a loud party. I want to recreate these two scenarios using LEDs, an analog sensor, and a digital switch.
Process
From very well mind ,the color blue is associated with the feeling of calmness or serenity. It is peaceful, tranquil, and secure which is great for sleep. Yet as a cool color, blue can sometimes seem icy, distant, or even cold. Hence can also lower the pulse rate and body temperature- the best conditions for sleep.
I didn’t have to do research for party LED, we can agree, we are going disco and loud.
This is how the setup is going to work. A dark room will stimulate the photosensor, turning on the blue led, it’s time to sleep. However, there is a button to switch to a party mode, with a lot of colorful lights.
Final Work
Challenges
I struggled for a long time in fixing the button to control the party LEDs. I tried different positions and connections on the breadboard to make it work. I tried to draw my circuit in an Arduino simulator Tinkercad but couldn’t get the hang of it. I had a lot of wires all over the breadboard, I needed a bigger breadboard.
Code
const int knob = A0; //button int buttonPin = 11; bool onOff = false; bool prevButtonState = LOW; //sleep const int led = 13; //party const int Led2=2; const int Led3=3; const int Led4=4; const int Led5=5; const int Led6=6; const int Led7=7; const int Led8=8; const int Led9=9; int i; int c; void setup() { Serial.begin(9600); pinMode(led, OUTPUT); pinMode(knob, INPUT); pinMode(buttonPin, INPUT); pinMode(Led2,OUTPUT); pinMode(Led3,OUTPUT); pinMode(Led4,OUTPUT); pinMode(Led5,OUTPUT); pinMode(Led6,OUTPUT); pinMode(Led7,OUTPUT); pinMode(Led8,OUTPUT); pinMode(Led9,OUTPUT); } void loop() { int knobValue = analogRead(knob); if (knobValue > 300){ digitalWrite(led,HIGH); } else{ digitalWrite(led,LOW); } bool currentState = digitalRead(buttonPin); if(currentState == HIGH && prevButtonState == LOW){ onOff = !onOff; Serial.println(onOff); if (onOff == true){ digitalWrite(led,LOW); party(); } else{ noParty(); } } prevButtonState = currentState; } void party(){ for(i=1;i< 100;i++) { if(i%2==0) { digitalWrite(Led2,HIGH); digitalWrite(Led4,HIGH); digitalWrite(Led6,HIGH); digitalWrite(Led8,HIGH); digitalWrite(Led3,LOW); digitalWrite(Led5,LOW); digitalWrite(Led7,LOW); digitalWrite(Led9,LOW); wait(100); } else { digitalWrite(Led3,HIGH); digitalWrite(Led5,HIGH); digitalWrite(Led7,HIGH); digitalWrite(Led9,HIGH); digitalWrite(Led2,LOW); digitalWrite(Led4,LOW); digitalWrite(Led6,LOW); digitalWrite(Led8,LOW); wait(100); } } for(i=0;i<100;i++) { for(c=2;c<=i/2;c++) { if(i%c==0) { digitalWrite(Led2,HIGH); digitalWrite(Led4,HIGH); digitalWrite(Led6,HIGH); digitalWrite(Led8,HIGH); digitalWrite(Led3,LOW); digitalWrite(Led5,LOW); digitalWrite(Led7,LOW); digitalWrite(Led9,LOW); wait((i*10)); } } if(c>i/2) { digitalWrite(Led3,HIGH); digitalWrite(Led5,HIGH); digitalWrite(Led7,HIGH); digitalWrite(Led9,HIGH); digitalWrite(Led2,LOW); digitalWrite(Led4,LOW); digitalWrite(Led6,LOW); digitalWrite(Led8,LOW); wait((i*10)); } } } void noParty(){ digitalWrite(Led2,LOW); digitalWrite(Led4,LOW); digitalWrite(Led6,LOW); digitalWrite(Led8,LOW); digitalWrite(Led3,LOW); digitalWrite(Led5,LOW); digitalWrite(Led7,LOW); digitalWrite(Led9,LOW); } void wait(int period){ long time_now = millis(); while(millis() < time_now + period){ //wait } }