const int LED_ROJO = A4; //Pin connected to red led const int LED_VERDE = A2; //Pin connected to green led const int LED_AZUL = A3; //Pin connected to blue led void setup() { // led's initialitation pinMode(LED_ROJO, OUTPUT); pinMode(LED_VERDE, OUTPUT); pinMode(LED_VERDE, OUTPUT); } void loop() { analogWrite(LED_ROJO, 0); //Red Colour ON delay(1000); analogWrite(LED_ROJO, 255); //Red Colour OFF analogWrite(LED_VERDE, 0); //Green Colour ON delay(1000); analogWrite(LED_VERDE, 255); //Green Colour OFF analogWrite(LED_AZUL, 0); //Blue Colour ON delay(1000); analogWrite(LED_AZUL, 255); //Blue Colour OFF delay(1000); }
//declaring rgb led pins const int LedPinGreen = A2; const int LedPinRed = A4; const int LedPinBlue=A3; //declaring variables sonar pins const int Echo = 6; const int Trigger = 5; void setup() { Serial.begin(9600); pinMode(LedPinGreen, OUTPUT);//rgb led pinMode(LedPinRed, OUTPUT); pinMode(LedPinBlue, OUTPUT); pinMode(Trigger, OUTPUT); //sonar pins pinMode(Echo, INPUT); } void loop() { int cm = ping(Trigger, Echo); Serial.print("Distancia: "); Serial.println(cm); delay(1000); } int ping(int Trigger, int Echo) { long duration, distanceCm; digitalWrite(Trigger, LOW); //para generar un pulso limpio ponemos a LOW 4us delayMicroseconds(4); digitalWrite(Trigger, HIGH); //generamos Trigger (disparo) de 10us delayMicroseconds(10); digitalWrite(Trigger, LOW); duration = pulseIn(Echo, HIGH); //medimos el tiempo entre pulsos, en microsegundos distanceCm = duration * 10 / 292/ 2; //convertimos a distancia, en cm return distanceCm; if(distanceCm>'10'){ analogWrite(LedPinRed,150);// if the distance of the distance sensor is bigger than 10cm, rgb led at (150, 150, 150) analogWrite(LedPinGreen,150); analogWrite(LedPinBlue, 150); } else{ analogWrite(LedPinRed,50);// if the distance of the distance sensor is smaller than 10cm, rgb led at (50, 75, 241). analogWrite(LedPinGreen,75); analogWrite(LedPinBlue, 241);// } }
#include <SoftwareSerial.h> #include <NewPing.h> //including library for ultrasonic sensor #define TRIGGER_PIN 5 // pin that send a signal #define ECHO_PIN 6 // pin that receive the signal #define TRIGGER_PIN2 8 // pin that send a signal #define ECHO_PIN2 7 // pin that receive the signal #define MAX_DISTANCE 400 // max distance that the sensor will read //initializing both sonars NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // establish the new ping telling which are the pins and the max distance NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE); // establish the new ping telling which are the pins and the max distance //initializing software serial SoftwareSerial mySerial(0, 1); //Tx and Rx pins (respectivamente) - cambiado, originalmente (0,1) //declaring led variable int redPin = A4; // Red Led Pin unsigned long rxTime; //define variable sensor 1 unsigned long rxTime2; //define variable sensor 2 float distance; float distance2; int bucle1=0; int bucle2=0; int bucle3=0; unsigned long previousMillis; unsigned long currentMillis; const long interval = 1500; /*float definition: * is a number that has a decimal point. It is used to approximate analog and continuous values * because they have greater resolution than integers. Floating numbers can be as large as *3.4028235E+38 and as low as -3.40282335E+38. *32-bit storage (4 bytes of information) *6-7 digits of precision * */ void setup() { // put your setup code here, to run once: //beginning baud rate for serial communication mySerial.begin(9600); //pin activation for sonar1 pinMode(ECHO_PIN,INPUT); pinMode(TRIGGER_PIN,OUTPUT); //pin activation for sonar2 pinMode(ECHO_PIN2, INPUT); pinMode(TRIGGER_PIN2, OUTPUT); //pin activation for red led pinMode(redPin, OUTPUT); // sets the pins as output } void loop() { // SONAR 1 READ // put your main code here, to run repeatedly: digitalWrite(TRIGGER_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIGGER_PIN, LOW); rxTime = pulseIn(ECHO_PIN, HIGH); //read the Receive time distance = (float)rxTime * 34 / 2000.0; //Converted into a distance ,cm // SONAR 2 READ digitalWrite(TRIGGER_PIN2, HIGH); delayMicroseconds(10); digitalWrite(TRIGGER_PIN2, LOW); rxTime2 = pulseIn(ECHO_PIN2, HIGH); //read the Receive time distance2 = (float)rxTime2 * 34 / 2000.0; //Converted into a distance ,cm /*FOUR POSSIBLE SITUATIONS WHEN READING BOTH SONARS: 1st sonar - left; 2nd sonar - right * 1st: 1>10 && 2>10 - neither sonars are activated - no movement expected * 2nd: 1<10 && 2>10 - activating left sonar - clockwise motor movement response (moving to the left) * 3rd: 1>10 && 2<10 - activating right sonar - counterclockwise motor response (moving to the right) * 4th: 1<10 && 2<10 - activating both sonars under 10cm - no movement expected */ //motor stepper messages /*For the slave board: * Number 1: do not move or stop moving * Number 2: move clockwise, moving the slider to the left * Number 3: move counterclockwise, moving the slider to the right * */ /*2*/ if ((distance <10) && (distance2>10)) { if ((bucle1==0) && (bucle2==0) && (bucle3==0)) { bucle2=1; } if ((bucle2==1) || (bucle3==1)) { bucle1=0; bucle2=0; bucle3=0; mySerial.println(2); bucle1=1; previousMillis = millis(); } } /*3*/ else if((distance>10) && (distance2<10)) { if ((bucle1==0) && (bucle2==0) && (bucle3==0)) { bucle3=1; } if ((bucle1==1) || (bucle3==1)){ bucle2=0; bucle1=0; bucle3=0; mySerial.println(3); bucle2=1; previousMillis=millis(); } } /*ambas*/ else if ((distance <10) && (distance<10)) { if ((bucle1==0) && (bucle2==0) && (bucle3==0)) { bucle1=1; } if((bucle2==1) || (bucle3==1)) { bucle3=0; bucle1=0; bucle2=0; mySerial.println(1); bucle3=1; previousMillis=millis(); } } else if((distance > 10) && (distance2 >10)) { currentMillis = millis(); if (currentMillis - previousMillis >= interval) { bucle1=0; bucle2=0; bucle3=0; } } }