Last active
July 15, 2020 05:44
-
-
Save juniorUsca/24a54d8130e9d7b50877e3a2de662718 to your computer and use it in GitHub Desktop.
Circuito comunicando 2 arduinos usando wire
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <IRremote.h> | |
#include <Servo.h> | |
IRrecv rc(8); | |
decode_results results; | |
Servo SER_M1; | |
Servo SER_M2; | |
Servo SER_M3; | |
int const PINO_SGAS = A1; | |
int LED_VERDE = 9; | |
int LED_AMARILLO = 10; | |
int LED_NARANJA = 11; | |
int LED_ROJO = 12; | |
int Sensor = 13; | |
float tempC; | |
int Sensortemp = A2; | |
int sensorBoc = 6; | |
int vib2=2; | |
int vib3=3; | |
void setup(){ | |
Serial.begin(9600); | |
Serial.flush(); | |
rc.enableIRIn(); | |
pinMode(LED_VERDE, OUTPUT); | |
pinMode(LED_AMARILLO, OUTPUT); | |
pinMode(LED_NARANJA, OUTPUT); | |
pinMode(LED_ROJO, OUTPUT); | |
pinMode(sensorBoc, OUTPUT); | |
pinMode(vib2, OUTPUT); | |
pinMode(vib3, OUTPUT); | |
SER_M1.attach(7); | |
SER_M2.attach(4); | |
SER_M3.attach(5); | |
SER_M1.write(0); | |
SER_M2.write(0); | |
SER_M3.write(0); | |
} | |
void loop() { | |
// analogWrite(6, 250); // en lugar de usar tone | |
// por conflictos con IRremote.h | |
// se puede usar analogWrite u otro arduino | |
if (rc.decode(&results)){ | |
switch(results.value){ | |
case 0xFD08F7://Boton 1 Sensor de Gas | |
openGas(); | |
break; | |
case 0xFD8877://Boton 2 Sensor de distancia (sonar) | |
openDist(); | |
break; | |
case 0xFD807F://Boton vol+ Sensor de temperatura | |
openTemp(); | |
break; | |
case 0xFD906F://Boton vol- Extraccion de muestra | |
openMuest(); | |
break ; | |
case 0xFD20DF://Boton l<< Limpieza por vibración y servo | |
openLimv(); | |
break; | |
} | |
rc.resume(); | |
} | |
} | |
void openGas() { | |
int valor = analogRead(PINO_SGAS); | |
valor = map(valor, 300, 750, 0, 100); | |
digitalWrite(LED_VERDE, HIGH); | |
digitalWrite(LED_AMARILLO, valor >= 30 ? HIGH : LOW); | |
digitalWrite(LED_NARANJA, valor >= 50 ? HIGH : LOW); | |
digitalWrite(LED_ROJO, valor >= 80 ? HIGH : LOW); | |
Serial.write(int('$')); // dolar es un indicador de gas | |
Serial.write(valor); | |
delay(250); | |
} | |
void openDist(){ | |
long duration, cm; | |
pinMode(Sensor, OUTPUT); | |
digitalWrite(Sensor, LOW); | |
delayMicroseconds(2); | |
digitalWrite(Sensor, HIGH); | |
delayMicroseconds(5); | |
digitalWrite(Sensor, LOW); | |
pinMode(Sensor, INPUT); | |
duration = pulseIn(Sensor, HIGH); | |
cm = microsecondsToCentimeters(duration); | |
Serial.print("Distancia: "); | |
Serial.print(cm); | |
Serial.print("cm"); | |
Serial.println(); | |
if(cm <= 80){ | |
digitalWrite(LED_VERDE, HIGH); | |
} | |
else{ | |
digitalWrite(LED_VERDE, LOW); | |
} | |
delay(100); | |
if (cm>80 && cm<150){ | |
digitalWrite(LED_AMARILLO, HIGH); | |
} | |
else{ | |
digitalWrite(LED_AMARILLO, LOW); | |
} | |
delay(100); | |
if (cm>150 && cm<220){ | |
digitalWrite(LED_NARANJA,HIGH); | |
} | |
else{ | |
digitalWrite(LED_NARANJA,LOW); | |
} | |
if (cm>=230){ | |
digitalWrite(LED_ROJO,HIGH); | |
} | |
else{ | |
digitalWrite(LED_ROJO,LOW); | |
} | |
} | |
long microsecondsToCentimeters(long microseconds) { | |
return microseconds / 29 / 1.576; | |
} | |
void openTemp() { | |
tempC = analogRead(Sensortemp); | |
tempC = (5.0 * tempC * 100.0)/1024.0; | |
tempC=tempC-51; | |
Serial.print("Temperatura: "); | |
Serial.print(tempC); | |
Serial.print("\n"); | |
} | |
void openMuest() { | |
SER_M1.write(50); | |
delay(1000); | |
SER_M1.write(0); | |
} | |
void openLimv(){ | |
SER_M2.write(100); | |
SER_M3.write(100); | |
delay(2000); | |
digitalWrite(vib2, HIGH); | |
digitalWrite(vib3, HIGH); | |
delay(3000); | |
digitalWrite(vib2, LOW); | |
digitalWrite(vib3, LOW); | |
SER_M2.write(0); | |
SER_M3.write(0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int readed = false; | |
int valor; | |
int type = 0; // 0 es ningun tipo | |
// 1 es tipo gas | |
void setup() | |
{ | |
Serial.begin(9600); | |
Serial.flush(); | |
pinMode(6, OUTPUT); | |
} | |
void loop() | |
{ | |
// tone(6, 320); // en lugar de usar tone en el otro arduino | |
// por conflictos con IRremote.h | |
// se puede usar tone aqui | |
if (Serial.available() > 0) { | |
valor = Serial.read(); | |
char envio = char(valor); | |
if (type != 0) { | |
readed = true; | |
} | |
if (envio == '$' && !readed) { | |
type = 1; | |
} | |
} | |
if (type == 1 && readed) { | |
Serial.println(valor); | |
// aqui podemos procesar la informacion | |
if (valor > 50) { | |
tone(6, 320); | |
} else { | |
noTone(6); | |
} | |
type = 0; | |
readed = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment