Arduino trafikljus för nybörjare (4 / 25 steg)
sensor_low = 1023;>
sensor_low = sensor_value
}1IHKHPOL8P.MEDIUM.jpg"> {
sensor_high = sensor_value
}/int SENSOR_PIN=A0;
int sensor_mid = 511;
int GREEN = 2;
int YELLOW = 7;
int RED = 12;
int STREET_LIGHTS = 13;
voidsetup()
{
pinMode(GREEN, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(STREET_LIGHTS, OUTPUT);
calibrate();
}
voidloop()
{
int sensor_value = analogRead(SENSOR_PIN);
Serial.println(sensor_value);
if(sensor_value < sensor_mid)
{
green_light();
}
else
{
red_light();
}
}
void calibrate()
{
int sensor_high = 0;
int sensor_low = 1023;
int current_time = millis();
int end_time = current_time + 5000;
// we have to calibrate this in the first five seconds
while (current_time < end_time)
{
// now update the current time
current_time = millis();
// update sensor value here!
// your code here
// record the minimum sensor value
if (sensor_value < sensor_low)
{
//your code here
}
// record the maximum sensor value
if//your code here
{
sensor_high = sensor_value;
}
}
//set mid to be the MIDDLE! :)
sensor_mid = (sensor_high + sensor_low)/2;
}
void all_LEDs_off()
{
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);
digitalWrite(STREET_LIGHTS, LOW);
}
void green_light()
{
all_LEDs_off();
digitalWrite(GREEN, HIGH);
}
void yellow_light()
{
all_LEDs_off();
digitalWrite(YELLOW, HIGH);
}
void red_light()
{
all_LEDs_off();
digitalWrite(RED, HIGH);
digitalWrite(STREET_LIGHTS, HIGH);
}TLH/HKHPOL8Q/FQ// variables for green light
int DELAY_GREEN = 1000;
int DELAY_YELLOW = 500;
int DELAY_RED = 1000;Change your loop function from the previous step to:TLHHKHPOL8Q.MEDIUM.jpg">
* HUR LÄNGE TILL KRYMPA BRO?
Verktyg som behövs:
-Händer
-Wire strippor
-Tråd Snips eller sax
-Lödkolv
-Värmepistol
Material som behövs:
-Löda
-1 x svart tråd ( voidloop()
{
green_light();
delay(DELAY_GREEN);
yellow_light();
delay(DELAY_YELLOW);
red_light();
delay(DELAY_RED);
} sensor_mid AWG, ~ boolean 5cm i längd)
-1 x röd tråd ( boolean true AWG, ~ false 5cm i längd)
-1 x fotocell
- int x Heatshrink tube (4 cm lång, boolean i_love_kittens = true; . i_love_kittens mm ( true / boolean my_awesome_function()
{
boolean i_love_kittens = true;
return i_love_kittens;
} boolean look_for_car()
{
int current_time = millis();
int end_time = current_time + 2000; // we'll look for about two seconds
boolean saw_car = false;
while(current_time < end_time)
{
sensor_value = analogRead(SENSOR_PIN);
if(sensor_value < sensor_mid)
{
// set the value of saw_car to true
// your code here
}
// update the current time
// your code here
}
return saw_car;
} ") diameter)
Det finns ingen g boolean saw_car = look_for_car(); animation här. Kanske kommer nästa steg!