Está en la página 1de 6

int analogPin = 0; int readValue = 0; float temperature = 0; float temperatureF = 0; void setup() { Serial.

begin(9600); } void loop() { readValue = analogRead(analogPin); //Serial.println(readValue); temperature = (readValue * 0.0049); temperature = temperature * 100; temperatureF = (temperature * 1. + 32; temperature = readValue; Serial.print("Luminosidade: "); Serial.println(temperature); if(temperature > 850) { Serial.println("Luz esta acesa"); } else { Serial.println("Luz esta apagada"); } Serial.println(""); delay(1000); }

#define LIGA HIGH #define DESLIGA LOW #define HORARIA 0 #define ANTIHORARIA 1 int ledPin[4]={9,10,11,12}; int maisPin=2,menosPin=3,rotPin=4; int curLed=0; int curRot=ANTIHORARIA; int speedLed=500; int speedStep=50; unsigned long curTime; void setup(){ int i; for(i=0;i<4;i++) { pinMode(ledPin[i],OUTPUT); digitalWrite(ledPin[i],DESLIGA); } pinMode(maisPin,INPUT); pinMode(menosPin,INPUT); pinMode(rotPin,INPUT); digitalWrite(ledPin[curLed],LIGA); curTime=millis(); Serial.begin(9600); }

void loop(){ char tecla; while(Serial.available() > 0) { tecla = Serial.read(); if(tecla == 'h') { curRot=HORARIA; ; delay(20); } if(tecla == 'a') { curRot=ANTIHORARIA; ; delay(20); } if(tecla == '+') { speedLed -= speedStep; if(speedLed < 40) speedLed = 40; ; delay(20); } if(tecla == '-') { speedLed += speedStep; if(speedLed > 2000) speedLed = 2000; ; delay(20); } } //teste se ja correu o tempo para o Led ligado if((millis()-curTime)>=speedLed) { // desligue o led atual digitalWrite(ledPin[curLed],DESLIGA); // mude para o proximo led if(curRot == ANTIHORARIA) { curLed++; if(curLed >3) curLed=0; } else { curLed--; if(curLed<0) curLed=3; } // ligue o novo led digitalWrite(ledPin[curLed],LIGA); curTime=millis(); } }

CORRIGIDO: #define LIGA HIGH #define DESLIGA LOW #define HORARIA 0 #define ANTIHORARIA 1 int ledPin[4]={9,10,11,12}; int maisPin=2,menosPin=3,rotPin=4; int curLed=0; int curRot=ANTIHORARIA; int speedLed=500; int speedStep=50; unsigned long curTime; void setup(){ int i; for(i=0;i<4;i++) { pinMode(ledPin[i],OUTPUT); digitalWrite(ledPin[i],DESLIGA); } pinMode(maisPin,INPUT); pinMode(menosPin,INPUT); pinMode(rotPin,INPUT); digitalWrite(ledPin[curLed],LIGA); curTime=millis(); } void loop(){ //teste se ja correu o tempo para o Led ligado if((millis()-curTime)>=speedLed) { // desligue o led atual digitalWrite(ledPin[curLed],DESLIGA); // mude para o proximo led if(curRot == ANTIHORARIA) { curLed++; if(curLed >3) curLed=0; } else { curLed--; if(curLed<0) curLed=3; } // ligue o novo led digitalWrite(ledPin[curLed],LIGA); curTime=millis(); } // TESTE SE O BOTAO DE MAIS ESTA PRESSIONADO if(digitalRead(maisPin)==LOW) { // aumentar velocidade de rotacao speedLed -= speedStep; if(speedLed < 40)

speedLed = 40; // espere ate tirar o dedo do botao while(digitalRead(maisPin) == LOW) ; // ponha um delay apos tirar o dedo para o debounce delay(20); } // TESTE SE O BOTAO DE MENOS ESTA PRESSIONADO if(digitalRead(menosPin)==LOW) { // aumentar velocidade de rotacao speedLed += speedStep; if(speedLed > 2000) speedLed = 2000; // espere ate tirar o dedo do botao while(digitalRead(menosPin) == LOW) ; // ponha um delay apos tirar o dedo para o debounce delay(20); } // TESTE SE O BOTAO DE ROTACAO ESTA PRESSIONADO if(digitalRead(rotPin)==LOW) { // aumentar velocidade de rotacao if(curRot ==HORARIA) curRot=ANTIHORARIA; else curRot=HORARIA; // espere ate tirar o dedo do botao while(digitalRead(rotPin) == LOW) ; // ponha um delay apos tirar o dedo para o debounce delay(20); } }

Sequencia de leds: byte ledPin [] = {1,2,3,4}; int botao1 = 11; int botao2 = 12; int botao3 = 13; int ledDelay(1000); int passo = 100; int direction = 1; int currentLed = 0; unsigned long changeTime; void setup(){ for(int x = 0; x<5; x++){ pinMode(ledPin[x], OUTPUT); } changeTime = millis(); pinMode (botao1, INPUT);

pinMode (botao2, INPUT); pinMode (botao3, INPUT); } void changeLed(){ for (int x=0; x<5; x++){ digitalWrite(ledPin[x],LOW); } digitalWrite(ledPin[currentLed],HIGH); currentLed += direction; if(currentLed==0){direction=1;} if(currentLed >3){currentLed=0;} } void loop(){ if((millis()-changeTime)>ledDelay) { changeLed(); changeTime=millis(); } if(digitalRead(botao1)==LOW){ ledDelay -= passo; if(ledDelay<50) ledDelay = 50; while(digitalRead(botao1)==LOW) ; delay(30); } if(digitalRead(botao2)==LOW){ ledDelay += passo; if(ledDelay>1000) ledDelay = 1000; while(digitalRead(botao2)==LOW) ; delay(30); } if(digitalRead(botao3)==LOW){ if (direction == 1) direction = 0; else direction = 1; } }

int LED = 9; int botaomais = 12; int botaomenos = 11; int fadevalue = 0; int passo = 5; int blinkled = 13; int ligadesliga = HIGH; void setup(){ pinMode(botaomais, INPUT); pinMode(botaomenos, INPUT); pinMode(blinkled, OUTPUT); }

void loop() { if(digitalRead(botaomais)==LOW) { fadevalue +=passo; if(fadevalue>255) fadevalue=255; analogWrite(LED,fadevalue); while(digitalRead(botaomais)==LOW) ; delay(30); } if(digitalRead(botaomenos)==LOW) { fadevalue -=passo; if(fadevalue<0) fadevalue=0; analogWrite(LED,fadevalue); while(digitalRead(botaomenos)==LOW) ; delay(30); } digitalWrite(blinkled, ligadesliga); delay(40); if (ligadesliga == HIGH) ligadesliga = LOW; else ligadesliga = HIGH; }

También podría gustarte