From ff4530420fcbe2f2330178bcdf36bebe5c95d3da Mon Sep 17 00:00:00 2001 From: nateriek <32182835+nateriek@users.noreply.github.com> Date: Tue, 29 Jan 2019 19:37:35 -0500 Subject: [PATCH] Add files via upload --- IRTest_oddSensors.ino | 152 ++++++++++++++++++++++++------------------ 1 file changed, 87 insertions(+), 65 deletions(-) diff --git a/IRTest_oddSensors.ino b/IRTest_oddSensors.ino index c42f671..0cd3681 100644 --- a/IRTest_oddSensors.ino +++ b/IRTest_oddSensors.ino @@ -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); + }