User:Bzeher/enes100/RemoteCar/IRScode

Here is working code for 3 IR sensors and the motors. This code can be modified to work with other sensors as well. All code can be copied into Arduino.


 * 1) define BRAKEVCC 0
 * 2) define CW  1
 * 3) define CCW 2
 * 4) define BRAKEGND 3
 * 5) define CS_THRESHOLD 100

int inApin[2] = {7, 4}; // INA: Clockwise input int inBpin[2] = {8, 9}; // INB: Counter-clockwise input int pwmpin[2] = {5, 6}; // PWM input int cspin[2] = {2, 3}; // CS: Current sense ANALOG input int enpin[2] = {0, 1}; // EN: Status of switches output (Analog pin) int statpin = 13;      //motor pin int sensePinR = 1;      //Right sensor int sensePinM = 2;     //Middle sensor int sensePinL = 3;      //Left sensor int sampleR = 0;       //number of samples int sampleM = 0; int sampleL = 0; int rsValue;           //value of each sample int msValue; int lsValue;

void setup { analogReference(DEFAULT); Serial.begin(9600); pinMode(statpin, OUTPUT); // Initialize digital pins as outputs for (int i=0; i<2; i++) {  pinMode(inApin[i], OUTPUT); pinMode(inBpin[i], OUTPUT); pinMode(pwmpin[i], OUTPUT); } // Initialize braked for (int i=0; i<2; i++) {  digitalWrite(inApin[i], LOW); digitalWrite(inBpin[i], LOW); } }

/* pmw value has to be < 1023. CCW=CounterClockwise CW=ClockWise -*/

void loop { motorGo(0, 0, 0);     //Keep straight motorGo(1, CW, 100);   //Forward sampleR=0;              //set samples to zero each loop sampleM=0; sampleL=0; //Sampling three sensors. for ( int i = 0; i<30; i++)       //Taking 30 samples from each sensor { rsValue = analogRead(1);        //Counting values less than 20 for Right sensor if(rsValue < 20) sampleR = sampleR + 1; msValue = analogRead(2);       //Counting values less than 20 for Middle sensor if(msValue < 20) sampleM = sampleM + 1; lsValue = analogRead(3);       //Counting values less than 20 for Left sensor if(lsValue < 20) sampleL = sampleL + 1; delay(1);                            //Sample period. } if(sampleR > 0  && sampleM > 0 && sampleL > 0 )     //All sensor detected {    motorGo(1, 0, 0);  //Stop                                          //Motor cotrol delay(300); motorGo(0, CW, 200); //Turn Right motorGo(1, CCW, 100); //Revearse delay(1000); motorGo(1, 0, 0); // Stop delay(300); motorGo(0, CCW, 200); //Turn Left motorGo(1, CW, 100); //Forward delay(1000); } if(sampleR > 0 && sampleM ==0 && sampleL==0 )     //Right sensor detected only {    motorGo(1, 0, 0);  //Stop                                          //Motor cotrol delay(300); motorGo(0, CW, 200); //Turn Right motorGo(1, CCW, 100); //Revearse delay(400); motorGo(1, 0, 0); // Stop delay(300); motorGo(0, CCW, 200); //Turn Left motorGo(1, CW, 100); //Forward delay(400); } if(sampleR == 0 && sampleM > 0 && sampleL == 0 )     //Middle sensor detected only {    motorGo(1, 0, 0);  //Stop                                          //Motor cotrol delay(300); motorGo(0, CW, 200); //Turn Right motorGo(1, CCW, 100); //Revearse delay(1000); motorGo(1, 0, 0); // Stop delay(300); motorGo(0, CCW, 200); //Turn Left motorGo(1, CW, 100); //Forward delay(1000); } //Threshold reached if ((analogRead(cspin[0]) < CS_THRESHOLD) && (analogRead(cspin[1]) < CS_THRESHOLD)) digitalWrite(statpin, HIGH); } I only wrote this for the Right and Middle sensor being detected. More if statements can be added for the Left sensors and all sensors being detected at once within the sample period.

void motorOff(int motor) { // Initialize braked for (int i=0; i<2; i++) {  digitalWrite(inApin[i], LOW); digitalWrite(inBpin[i], LOW); } analogWrite(pwmpin[motor], 0); }

void motorGo(uint8_t motor, uint8_t direct, uint8_t pwm) { if (motor <= 1) {  if (direct <=4) {    // Set inA[motor] if (direct <=1) digitalWrite(inApin[motor], HIGH); else digitalWrite(inApin[motor], LOW);

// Set inB[motor] if ((direct==0)||(direct==2)) digitalWrite(inBpin[motor], HIGH); else digitalWrite(inBpin[motor], LOW); analogWrite(pwmpin[motor], pwm); } } }