top of page
Foto do escritorLM

Alarme de proximidade de lixo

cool, criado em arduino

not working 100%


#define POT A0 // V0 azul longo #define BOT 2 // D2 amarelo #define LED 4 // D4 azul curto #define BUZ 5 // D5 ~PWM laranja #define ECHO 6 // D6 ~PwM branco #define TRIG 7 // D7 verde //---------------------------------------------- bool botON = false; void setup(){ Serial.begin(9600); pinMode(BOT, INPUT_PULLUP); pinMode(LED, OUTPUT); pinMode(BUZ, OUTPUT); pinMode(ECHO, INPUT); pinMode(TRIG, OUTPUT); Serial.println("Press to start"); while(digitalRead(BOT) == HIGH){ //Serial.println("Press to start"); } botON = true; Serial.println("Starting..."); } void loop() { unsigned long int t0, t; unsigned int dist, freq; if(botON){ t0 = millis(); //------------------------------------------------------ freq = map(analogRead(POT), 0, 1023, 0, 255); if(lerSonar() < 100){ // dist em cm, se algo esta a menos de 1m do sensor: t = millis(); //analogWrite(BUZ, freq); tone(BUZ, freq, 1500); //freq/(map(dist,5,50, 200, 5)/10.0) digitalWrite(LED, HIGH); while(t - millis() < 1500){} digitalWrite(LED, LOW); } //------------------------------------------------------ while(t0 - millis() < 5000){//Serial.print("."); } // dar algum delay porque se nao o botao nao tem tempo de levantar //Serial.println(""); isBotPressed(); } else{ //restart(); } } float lerSonar(){ unsigned long int t0 = micros(); digitalWrite(TRIG, HIGH); while(t0 - micros() < 10){} digitalWrite(TRIG, LOW); //unsigned int width = pulseIn(ECHO, HIGH); float distance = microsecondstoCentimeters(pulseIn(ECHO, HIGH)); // width //distance = constrain(distance, 0.05, 5); //Serial.println("distance:"); Serial.println(distance); return distance; // mm } long microsecondstoCentimeters(long microseconds){ return microseconds / 29 / 2; } void restart(){ //Serial.println("Press to restart"); if(digitalRead(BOT) == LOW){ botON = true; Serial.println("reStarting..."); } } void isBotPressed(){ //Serial.println("Press to stop"); if(digitalRead(BOT) == LOW){ botON = false; Serial.println("Stoping..."); } }

9 visualizações0 comentário

Comments


bottom of page