Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 87 additions & 65 deletions IRTest_oddSensors.ino
Original file line number Diff line number Diff line change
@@ -1,66 +1,88 @@
int AnalogInputPin0 = A0;
//int AnalogInputPin2 = A0;
int AnalogInputPin1 = A1;
//int AnalogInputPin4 = A3;
int AnalogInputPin2 = A2;
//int AnalogInputPin6 = A5;
int AnalogInputPin3 = A3;
//int AnalogInputPin8 = A7;
int AnalogInputPin4 = A4;

// raw IR values
int ir1 = 0;
int ir3 = 0;
int ir5 = 0;
int ir7 = 0;
int ir9 = 0;
int pitch = 4; //distance between IR sensors in mm

int sensor_dist = 0;
float error = 0;

//calculates distance between a given sensor and the middle of the array board
//only for IR arrays with an odd number of sensors
int sensor_distance(int sensorNum) {
//sensorNum is the number of the sensor counting from left to right
//5 is the ir in the middle of the array
sensor_dist = pitch*(5-sensorNum);
return sensor_dist ;
}

// calculates how far the line is from the center of the robot with a weighted average
float calculateError(){
int num = (sensor_distance(9)*(ir9-ir1) + sensor_distance(7)*(ir7-ir3));
int den = ir1+ir3+ir5+ir7+ir9;

error = num/den;
return error;
}

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(AnalogInputPin0, INPUT);
pinMode(AnalogInputPin1, INPUT);
pinMode(AnalogInputPin2, INPUT);
pinMode(AnalogInputPin3, INPUT);
pinMode(AnalogInputPin4, INPUT);

}


void loop() {
// put your main code here, to run repeatedly:
ir1 = analogRead(AnalogInputPin0);
ir3 = analogRead(AnalogInputPin1);
ir5 = analogRead(AnalogInputPin2);
ir7 = analogRead(AnalogInputPin3);
ir9 = analogRead(AnalogInputPin4);

calculateError();

Serial.println(ir1);

delay(100);

int AnalogInputPin0 = A0;
//int AnalogInputPin2 = A0;
int AnalogInputPin1 = A1;
//int AnalogInputPin4 = A3;
int AnalogInputPin2 = A2;
//int AnalogInputPin6 = A5;
int AnalogInputPin3 = A3;
//int AnalogInputPin8 = A7;
int AnalogInputPin4 = A4;

// raw IR values
int ir1 = 0;
int ir3 = 0;
int ir5 = 0;
int ir7 = 0;
int ir9 = 0;
int pitch = 4; //distance between IR sensors in mm

int sensor_dist = 0;
float error = 0;

// calibration ranges
int low1;
int high1;

//calculates distance between a given sensor and the middle of the array board
//only for IR arrays with an odd number of sensors
int sensor_distance(int sensorNum) {
//sensorNum is the number of the sensor counting from left to right
//5 is the ir in the middle of the array
sensor_dist = pitch*(5-sensorNum);
return sensor_dist ;
}

// calculates how far the line is from the center of the robot with a weighted average
float calculateError(){
int num = (sensor_distance(9)*(ir9-ir1) + sensor_distance(7)*(ir7-ir3));
int den = ir1+ir3+ir5+ir7+ir9;

error = num/den;
return error;
}

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(AnalogInputPin0, INPUT);
pinMode(AnalogInputPin1, INPUT);
pinMode(AnalogInputPin2, INPUT);
pinMode(AnalogInputPin3, INPUT);
pinMode(AnalogInputPin4, INPUT);
calibration();
}

void calibration(){
int start = millis();
int current = millis();
low1 = analogRead(AnalogInputPin0);
high1 = low1;
int value1;
while((current-start)<5000){
value1 = analogRead(AnalogInputPin0);
if(value1 > high1){
high1 = value1;
}
else if(value1 < low1){
low1 = value1;
}
current = millis();
}
}

void loop() {
// put your main code here, to run repeatedly:
ir1 = analogRead(AnalogInputPin0);
ir3 = analogRead(AnalogInputPin1);
ir5 = analogRead(AnalogInputPin2);
ir7 = analogRead(AnalogInputPin3);
ir9 = analogRead(AnalogInputPin4);

ir1 = map(ir1,low1,high1,0,100);
ir1 = constrain(ir1,0,100);

Serial.println(ir1);

delay(100);

}