Hi, here's a project for Arduino Uno R3 project with 5 LED lights, switched according to light level that is sensed by a photo-resistor. Program displays a NightRider (back-and-forth) effect in full light and an ambulance effect in lower light. Speed of either effect is affected by the light level (faster under more light).
Video: https://www.youtube.com/watch?v=wnpCkznqygg
Code below...
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
*/
int led1 = 12;
int led2 = 11;
int led3 = 10;
int led4 = 9;
int led5 = 8;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
if ( sensorValue > 785 )
{
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led3, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led4, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led3, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led5, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led4, LOW); // turn the LED off by making the voltage LOW
delay(10000/sensorValue); // wait
digitalWrite(led5, LOW); // turn the LED off by making the voltage LOW
delay(0); // wait
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
if ( sensorValue > 800 )
{
digitalWrite(led5, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led4, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led5, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led3, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led4, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led3, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
delay(10000/sensorValue); // wait
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
delay(0); // wait
}
}
else {
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led4, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led3, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led5, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led1, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(led3, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(led5, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, HIGH); // turn the LED off by making the voltage LOW
digitalWrite(led4, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}
}
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
*/
int led1 = 12;
int led2 = 11;
int led3 = 10;
int led4 = 9;
int led5 = 8;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
if ( sensorValue > 785 )
{
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led3, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led4, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led3, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led5, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led4, LOW); // turn the LED off by making the voltage LOW
delay(10000/sensorValue); // wait
digitalWrite(led5, LOW); // turn the LED off by making the voltage LOW
delay(0); // wait
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
if ( sensorValue > 800 )
{
digitalWrite(led5, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led4, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led5, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led3, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led4, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led3, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
delay(10000/sensorValue); // wait
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
delay(0); // wait
}
}
else {
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led4, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led3, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led5, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
digitalWrite(led1, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(led3, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(led5, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, HIGH); // turn the LED off by making the voltage LOW
digitalWrite(led4, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100000/sensorValue); // wait
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}
}
No comments:
Post a Comment