//a1,a2 = LOW,HIGH --->clockwise //a1,a2 = HIGH,LOW --->anti-clockwise //b1,b2 = LOW,HIGH --->anti-clockwise //b1,b2 = HIGH,LOW --->clockwise //a1,a2,b1,b2 = LOW,LOW,LOW,LOW --->brake // the enable pins are directly set to HIGH #include //This Library is for wireless rx and tx modules #define a1 2 //RIGHT WHEEL #define a2 3 #define b2 4 //LEFT WHEEL #define b1 5 #define receive_msg 9 #define gnd 11 #define vcc 8 int a = 0; // The average value of four divisions as shown in picture 2 int b = 0; int c = 0; int d = 0; #define leftButton 7 //the stepper motor trigger switch boolean lastButtonState = LOW; boolean currentButtonState = LOW; // Used in the debounce function boolean stepperOn = LOW; //Signifies stepper is ON or not int sensor_f[64]; //Array for the front HC-SR04 int sensor_r[64]; //Array fo the rear HC-SR04 void setup() { for(int i=2;i<6;i++) pinMode(i,OUTPUT); delay(1000); // Just to stop for startup Serial.begin(9600); //pinMode(led,OUTPUT); //uncomment it if you have a LED vw_set_rx_pin(receive_msg); // Set the pin as RX to receive message vw_setup(2400); // TO setup the baud rate vw_rx_start(); } boolean debounce(boolean last) // This function works the magic... { boolean current = digitalRead(leftButton); if (last != current) { current = digitalRead(leftButton); } return current; } void get_msg(int i) { byte buf[VW_MAX_MESSAGE_LEN]; // 'buf' array to store the message.......you can change the 'byte' to 'uint8_t'......if you dont know what does 'uint8_t' means plzz visit http://blog.oscarliang.net/arduino-difference-byte-uint8-t-unsigned-cha/ byte buflen = VW_MAX_MESSAGE_LEN; // To store the message length in buflen if(vw_get_message(buf, &buflen)) //this switches true if any message is received ......I dont really known why the pointer operator is required { int convt_f = int(buf[0]); // Converting the distance to integer type because the HC-SR04 sends the value from 0-200.....and byte = 2^7 = 0 to 127 int convt_r = int(buf[1]); if(convt_f < 0) // This is required coz if the value 'convt' is greater than 127 then it will turn -ve........ e.g. int = 128,129,130.....,200 ---> byte = -128,-127,-126...... ,-(200-256)[ i.e. 128+128=256 { int convt_f = int(buf[0])+256; sensor_f[i] = convt_f; } if(convt_r < 0) { int convt_r = int(buf[1])+256; sensor_r[i] = convt_r; } } } void loop() { currentButtonState = debounce(lastButtonState); if(currentButtonState == HIGH && lastButtonState == LOW) // this means someone has just pressed the switch { stepperOn = !stepperOn; if(stepperOn == HIGH) { Serial.println("g"); // print 'g' to go delay(5150); // 5150 milisec delay i.e 5.150 second for one rotation to make a dummy roatation just to build the pace } else // means if(stepperOn == HIGH) { Serial.println("s"); // Print 's' to stop } } if(stepperOn == HIGH) { for(int i=0;i<64;i++) // this for loop should take approx 4.096 + function run time ....function run time will be too small compared to the full rotation time so it can be neglected for now get_msg(i); // so we will get exactly 4 seconds to toggle the wheels and interact with the data given calculate(); instruct(); } lastButtonState = currentButtonState; } void calculate() // This funtion is quite easy to understand....just do it yourself { a = 0; b = 0; c = 0; d = 0; for(int i=0;i<16;i++) a = a + sensor_f[i]; for(int i=31;i<48;i++) a = (a + sensor_r[i]) / 32; for(int i=16;i<32;i++) b = b + sensor_f[i]; for(int i=48;i<64;i++) b = (b + sensor_r[i]) / 32; for(int i=32;i<48;i++) c = c + sensor_f[i]; for(int i=0;i<16;i++) c = (c + sensor_r[i]) / 32; for(int i=48;i<64;i++) d = d + sensor_f[i]; for(int i=16;i<32;i++) d = (d + sensor_r[i]) / 32; } void left() { //To Turn LEFT digitalWrite(b1,HIGH); //clockwise digitalWrite(b2,LOW); digitalWrite(a1,LOW); //clockwise digitalWrite(a2,HIGH); delay(1000); } void right() { //To Turn RIGHT digitalWrite(b1,LOW); //anti - clockwise digitalWrite(b2,HIGH); digitalWrite(a1,HIGH); //anti - clockwise digitalWrite(a2,LOW); delay(1000); } void forward() { //To ADVANCE digitalWrite(b1,LOW); //anti - clockwise digitalWrite(b2,HIGH); digitalWrite(a1,LOW); //clockwise digitalWrite(a2,HIGH); delay(1000); } void reverse() { //To REVERSE digitalWrite(b1,HIGH); //clockwise digitalWrite(b2,LOW); digitalWrite(a1,HIGH); //anti - clockwise digitalWrite(a2,LOW); delay(1000); } void halt() { //To STOP digitalWrite(b1,LOW); //Stops digitalWrite(b2,LOW); digitalWrite(a1,LOW); //Stops digitalWrite(a2,LOW); delay(250); } void instruct() // Check the the 2nd and 3rd image to understand this function { if(a > 25 && b > 25 && c > 25 && d > 25) { forward(); } if(a > 25 && b > 25 && c > 25 && d < 25) { halt(); right(); forward(); } /* if(a > 25 && b > 25 && c < 25 && d > 25) // Not required { halt(); right(); forward(); } */ if(a > 25 && b > 25 && c < 25 && d < 25) { halt(); right(); forward(); } /* if(a > 25 && b < 25 && c > 25 && d > 25) // not required { halt(); left(); forward(); } */ if(a > 25 && b < 25 && c > 25 && d < 25) { halt(); right(); forward(); } if(a > 25 && b < 25 && c < 25 && d > 25) { forward(); } if(a > 25 && b < 25 && c < 25 && d < 25) { halt(); right(); forward(); } if(a < 25 && b > 25 && c > 25 && d > 25) { halt(); right(); forward(); } if(a < 25 && b > 25 && c > 25 && d < 25) { halt(); reverse(); left(); forward(); } if(a < 25 && b > 25 && c < 25 && d > 25) { halt(); left(); forward(); } if(a < 25 && b > 25 && c < 25 && d < 25) { halt(); reverse(); right(); forward(); } if(a < 25 && b < 25 && c > 25 && d > 25) { halt(); left(); forward(); } if(a < 25 && b < 25 && c > 25 && d < 25) { halt(); reverse(); left(); forward(); } if(a < 25 && b < 25 && c < 25 && d > 25) { halt(); left(); forward(); } if(a < 25 && b < 25 && c < 25 && d < 25) { halt(); } }