Skip to content

Instantly share code, notes, and snippets.

@m4hi2
Created September 27, 2017 10:47
Show Gist options
  • Save m4hi2/cff3c1be126bb073527d9bf6b2bf83bd to your computer and use it in GitHub Desktop.
Save m4hi2/cff3c1be126bb073527d9bf6b2bf83bd to your computer and use it in GitHub Desktop.
int mid = 0;
int mn = 0;
int mx = 0;
int totalAnalogPinInUse = 9; //Please specify the total number of pins you used for the IR array
void setup() {
//put other setups
//calibrating the sensors
for(int i = 0; i<5000; i++){
digitalWrite(13, HIGH); //just to let you know the sensors are calibrating
int val = 0;
for(int j = 0; j<totalAnalogPinInUse; j++){
val = analogRead(j);
if (val>=mx){
mx = val;
}
if (val <= mn){
mn = val;
}
}
delay(1);
}
mid = ((mx + mn)/2);
digitalWrite(13, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
int s0 = 0;
int s1 = 0;
int s2 = 0;
int s3 = 0;
int s4 = 0;
int s5 = 0;
int s6 = 0;
int s7 = 0;
int s8 = 0;
s0 = analogRead(0);
s1 = analogRead(1);
s2 = analogRead(2);
s3 = analogRead(3);
s4 = analogRead(4);
s5 = analogRead(5);
s6 = analogRead(6);
s7 = analogRead(7);
s8 = analogRead(8);
delay(1);
if((((s0+s1+s2+s3)/4) > ((s5+s6+s7+s8)/4))){
//put your motor control code here. for moving right
}
if((((s0+s1+s2+s3)/4) < ((s5+s6+s7+s8)/4))){
//put your motor control code here. for moving left
}
if ((s3+s4+s5)/3 > 500){
//motor control for forward
}
if ((s3+s4+s5)/3 < mid){
//motor control for stop
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment