Olá turma,
Segue código final do semáforo para vocês
analisarem e brincarem
Obs.: O código está funcional,
mas pode ser melhorado!!
// os números são os pinos que controlam os leds nas suas respectivas cores
// give it a name:
int ledVermelho = 10;
int ledAmarelo = 11;
int ledVerde = 12;
int tempo = 1500;
// the setup routine runs once when you press reset:
//configura os pinos do Arduino como saida(OUTPUT)
void setup() {
// saida digital
pinMode(ledVermelho, OUTPUT);
pinMode(ledAmarelo, OUTPUT);
pinMode(ledVerde, OUTPUT);
//entrada digital, botões
pinMode(8, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
}
// the loop routine runs over and over again forever:
void loop() {
verde();
amarelo();
vermelho();
//tomar uma decisão
mudarTempo();
}
//função verde
void verde(){
digitalWrite(ledVerde, HIGH); // turn the LED on (HIGH is the voltage level)
delay(tempo); // espera 1 segundo
digitalWrite(ledVerde, LOW); // turn the LED off by making the voltage LOW - apaga o led
delay(10); //tempo curto
}
void amarelo(){
digitalWrite(ledAmarelo, HIGH); // turn the LED on (HIGH is the voltage level)
delay(tempo); // wait for a second
digitalWrite(ledAmarelo, LOW); // turn the LED off by making the voltage LOW
//delay(tempo);
}
//função vermelho
void vermelho(){
digitalWrite(ledVermelho, HIGH); // turn the LED on (HIGH is the voltage level)
delay(tempo); // wait for a second
digitalWrite(ledVermelho, LOW); // turn the LED off by making the voltage LOW
//delay(tempo);
}
//função mudarTempo()
void mudarTempo(){
if (digitalRead(8)==LOW){
tempo=2000;
}
if (digitalRead(7)==LOW){
tempo=300;
}
}
Nenhum comentário:
Postar um comentário