Repeat a task in a loop seprate to everything else in the loop

I would like to get my code to read the silver value then once it has done that go foward and spin around until it has found the can. The only way I have thought of doing this in by getting the robot to stop every .1 or so of a second and check to see the the can is in its range and if it is go foward but I don't know how to do this as I don't want the rest of the loop functioning while it is doing this,

Any help would be greatly appreciated, Thanks

#define S1 12 #define S0 13 #define S3 9 #define S2 19 #define sensorOut 8 #include "Range.h" #include "Timer.h" Range range(6, 7); Timer timerLoop; int frequency = 0; int readColor; int S_A = 10; int M_A1 = 2; int M_A2 = 3; int M_B1 = 4; int M_B2 = 5; int S_B = 10; int R = frequency; int G = frequency; int B = frequency; int W_S = A4; //Water Avoidence Sensor int R_S = A0; int C_S = A1; int L_S = A2; int O_S = A3; //Reflective Oil Spill int color = 0; void setup() { Serial.begin(9600); pinMode(S0, OUTPUT); pinMode(S1, OUTPUT); pinMode(S2, OUTPUT); pinMode(S3, OUTPUT); pinMode(sensorOut, INPUT); // Setting frequency-scaling to 20% digitalWrite(S0, HIGH); digitalWrite(S1, LOW); pinMode(M_B1, OUTPUT); pinMode(M_B2, OUTPUT); pinMode(M_A1, OUTPUT); pinMode(M_A2, OUTPUT); pinMode(S_B, OUTPUT); pinMode(S_A, OUTPUT); pinMode(L_S, INPUT); pinMode(C_S, INPUT); pinMode(R_S, INPUT); pinMode(W_S, INPUT); analogWrite(S_A, 150); //motor speed max 255 analogWrite(S_B, 150); delay(200); digitalRead(A5); // Setting the sensorOut as an input pinMode(sensorOut, INPUT); // Setting frequency scaling to 20% digitalWrite(S0, HIGH); digitalWrite(S1, LOW); // Begins serial communication Serial.begin(9600); } void loop() { if ((digitalRead(L_S) == 0) && (digitalRead(C_S) == 1) && (digitalRead(R_S) == 0)) { forward(); //0 is off 1 is on } else if ((digitalRead(W_S) == 0)) { water_Bottle(); //if water sensor sensors close then got to water bottle program (Round the obstacle) } else if ((digitalRead(L_S) == 1) && (digitalRead(C_S) == 1) && (digitalRead(R_S) == 0)) { turnLeft(); //0 is white 1 is black } else if ((digitalRead(L_S) == 1) && (digitalRead(C_S) == 0) && (digitalRead(R_S) == 0)) { turnLeft(); } else if ((digitalRead(L_S) == 0) && (digitalRead(C_S) == 1) && (digitalRead(R_S) == 1)) { turnRight(); } else if ((digitalRead(L_S) == 0) && (digitalRead(C_S) == 0) && (digitalRead(R_S) == 1)) { turnRight(); } else if ((digitalRead(L_S) == 1) && (digitalRead(C_S) == 1) && (digitalRead(R_S) == 1)) { Stop(); } // else if ((digitalRead(O_S) == 1)) {oil_spill();} //If this IR sensor sees the tin foil then go to the final program if (R<18 & R>11 & G<17 & G>132) { color = 1; // Black Serial.print("Black"); } if (G<6 & G>3 & B<5 & B>0) { color = 2; // White Serial.print("White"); } if (R<21 & R>17 & G<14 & G>10) { color = 3; // Green Serial.print("Green"); green_turn(); } if (R<6 & R>0 & G<5 & G>0) { color = 4; // Silver //When it sees the siver strip it goes foward, spins around, finds the can and goes foward Serial.print(" - Silver"); Serial.println(" - Rescue Time!!!!!"); digitalWrite(M_A1, LOW); digitalWrite(M_A2, HIGH);//fowards for 1 sec digitalWrite(M_B1, HIGH); digitalWrite(M_B2, LOW); delay(1000); Serial.print("Green Oil Spill"); Serial.print(range.sensorRange()); Serial.println("Searching........"); digitalWrite(M_A1, LOW); digitalWrite(M_A2, HIGH);//left until it sees can digitalWrite(M_B1, LOW); digitalWrite(M_B2, LOW); //Set a delay to read in a amount of a time then take a reading, then repeat until found the can, How would I accomplish this? This is the only way I can think of it working if it essentially stops every time the reads if the can is there go foward if not keep going round if (range.sensorRange() < 20) { Serial.println("Can Detected!!!!!!"); digitalWrite(M_A1, LOW); digitalWrite(M_A2, LOW);//stop for 3 sec digitalWrite(M_B1, LOW); digitalWrite(M_B2, LOW); delay(3000); digitalWrite(M_A1, LOW); digitalWrite(M_A2, HIGH);//fowards for 2 sec digitalWrite(M_B1, HIGH); digitalWrite(M_B2, LOW); delay(2000); } } color = readColor; delay(10); switch (color) { } color = 0; } void readColor1() { digitalWrite(S2, LOW); digitalWrite(S3, LOW); // Reading the output frequency frequency = pulseIn(sensorOut, LOW); Serial.print("R= "); Serial.print(frequency); Serial.print(" "); delay(50); digitalWrite(S2, HIGH); digitalWrite(S3, HIGH); // Reading the output frequency frequency = pulseIn(sensorOut, LOW); Serial.print("G= "); Serial.print(frequency); Serial.print(" "); delay(50); digitalWrite(S2, LOW); digitalWrite(S3, HIGH); // Reading the output frequency frequency = pulseIn(sensorOut, LOW); Serial.print("B= "); Serial.print(frequency); Serial.println(" "); delay(50); return color; } void forward() { digitalWrite(M_A1, LOW); //off digitalWrite(M_A2, HIGH); //on digitalWrite(M_B1, HIGH); //on digitalWrite(M_B2, LOW); //off } void turnRight() { digitalWrite(M_A1, LOW); digitalWrite(M_A2, LOW); digitalWrite(M_B1, HIGH); digitalWrite(M_B2, LOW); } void turnLeft() { digitalWrite(M_A1, LOW); digitalWrite(M_A2, HIGH); digitalWrite(M_B1, LOW); digitalWrite(M_B2, LOW); } void Stop() { digitalWrite(M_A1, LOW); digitalWrite(M_A2, LOW); digitalWrite(M_B1, LOW); digitalWrite(M_B2, LOW); } void water_Bottle() { digitalWrite(M_A1, HIGH); digitalWrite(M_A2, LOW);//backwards for .7 sec digitalWrite(M_B1, LOW); digitalWrite(M_B2, HIGH); delay(800); digitalWrite(M_A1, LOW); digitalWrite(M_A2, LOW);//turn left for .5 sec digitalWrite(M_B1, HIGH); digitalWrite(M_B2, LOW); delay(500); digitalWrite(M_A1, LOW); digitalWrite(M_A2, HIGH);//fowards for 1 sec digitalWrite(M_B1, HIGH); digitalWrite(M_B2, LOW); delay(1300); digitalWrite(M_A1, LOW); digitalWrite(M_A2, HIGH); digitalWrite(M_B1, LOW);//turn right for .5 sec digitalWrite(M_B2, LOW); delay(600); digitalWrite(M_A1, LOW); digitalWrite(M_A2, HIGH);//fowards for 1 sec digitalWrite(M_B1, HIGH); digitalWrite(M_B2, LOW); delay(1000); digitalWrite(M_A1, LOW); digitalWrite(M_A2, HIGH); digitalWrite(M_B1, LOW);//turn right for .5 sec digitalWrite(M_B2, LOW); delay(300); digitalWrite(M_A1, LOW); digitalWrite(M_A2, HIGH);//fowards for 1 sec digitalWrite(M_B1, HIGH); digitalWrite(M_B2, LOW); delay(100); } void green_turn() { digitalWrite(M_A1, LOW); digitalWrite(M_A2, LOW); digitalWrite(M_B1, HIGH); digitalWrite(M_B2, LOW); delay(200); } 

Touch here for the full post on the Arduino Apprentices tumblr

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s