#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; } } }
/* Stepper Motor Control - one revolution This program drives a unipolar or bipolar stepper motor. The motor is attached to digital pins 8 - 11 of the Arduino. The motor should revolve one revolution in one direction, then one revolution in the other direction. Created 11 Mar. 2007 Modified 30 Nov. 2009 by Tom Igoe June 5th Modified By Juan GM */ //project's code organisation for serial communication /*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 */ /*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 * */ //including libraries #include <SoftwareSerial.h> #include <Stepper.h> //intializing software serial SoftwareSerial mySerial(1, 0); //Rx and Tx pins (respectivamente) //declaring variables const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution // nema 14: 200 steps per revolution int stopMoving = 0; long steps = 50; //3cm each //distance variables for coordinates long lastPosition; long newPosition; const long totalDistance = 1200; const int origin = 0; //declaring led variable int redPin = A4; // Red Led Pin // initialize the stepper library on pins 5 through 8: Stepper myStepper(stepsPerRevolution, 5, 6, 7, 8); /*Function Stepper - 4 ways of doing it: * Stepper (steps, pin1, pin2): * steps: number of steps in ONE REVOLUTION. * * Stepper (steps, pin1, pin2, pin3, pin4) * * setSpeed (rpm): this function does not make the motor turn, it just sets the speed * at which it will turn when called a step * * step(steps) * */ void setup() { // set the speed at 60 rpm: myStepper.setSpeed(60); // initialize the serial port: mySerial.begin(9600); pinMode(redPin, OUTPUT); digitalWrite(redPin, LOW); lastPosition = 0; } void loop() { char data; //declaring receiving variable if (mySerial.available() ) { //if myserial is available, read data=mySerial.read(); if (data =='1') { /*digitalWrite(redPin, HIGH); delay(2000); digitalWrite(redPin, LOW); delay(2000);*/ myStepper.step(stopMoving); /* analogWrite(redPin, LOW);//rhythm to tell the one doing it to stop it. delay(500); analogWrite(redPin, HIGH); delay(500);*/ } else if (data == '2'){ /*turning clockwise - moving the slider to the left - (2) */ newPosition = lastPosition - steps; if (newPosition>origin && newPositionorigin && newPosition