Hi everyone!
So I'm making a robot using the FindBall Open IR sensor. It works great! But it can only give directions between 1 and 5, making it somewhat inaccurate, not to mention when the strengths of each sensor are so close to each other they jump wildly all over the place.
So.. I created (or tried to) a program that is supposed to using the strengths of two the strongest sensors, giving us a reading between 1 and 9. But the maths just aint working and its killing me!. My brain doesn't function anymore...
Heres the full code:
/*
Written by Julicrisby2017
Its not too good.
The sensor this refers too is the FindBall Open from Tris10.com*/
#include <Array.h>
#include <I2C.h>
#include <Wire.h>
#include <math.h>
//#include <HMC5883.h>
#include <ADXL335.h> // ignore these libraries down here, they are for the parts of my robot present in the full program.
#include <HMC5883L.h>
#include <I2Cdev.h>
#include <HMC5883L.h>#define FB_ADDR_7BIT (0x02 >> 1) // Here 0x02 gets bit-shifted to 0x01, I dont know why. FindBall 7-Bit Address is 0x01
unsigned char read_byte(unsigned char addr, unsigned char reg) { //Findball Open's IIC commandline
Wire.beginTransmission(addr); //The FindBall Open board has 5 individual IR sensors on it.
Wire.write(reg);//ranges from 0x42 -> 0x48. 0x42 returns the best sensor (1, 2, 3,4 or 5), 0x43 returns that sensors strength
Wire.endTransmission(); // 0x44 -> 0x48 return the strengths of the individual sensors. 0x44 being sensor 1 on the left, 0x48 being sensor 5 on the rightWire.requestFrom((uint8_t) addr, (uint8_t) 1);
if (Wire.available()) {
return Wire.read(); // succeeeed
} else {
return 255; // error
}
}void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin(); //so we can talk on the IIC bus
}void loop() {
// put your main code here, to run repeatedly:
int dir, maxa, max2, wantedpos, wantedpos2;
int sen1 = read_byte(FB_ADDR_7BIT, 0x44);
Serial.println("Sensor 1: ");
Serial.println(sen1);
int sen2 = read_byte(FB_ADDR_7BIT, 0x45);
Serial.println("Sensor 2: ");
Serial.println(sen2);
int sen3 = read_byte(FB_ADDR_7BIT, 0x46);
Serial.println("Sensor 3: ");
Serial.println(sen3);
int sen4 = read_byte(FB_ADDR_7BIT, 0x47);
Serial.println("Sensor 4: ");
Serial.println(sen4);
int sen5 = read_byte(FB_ADDR_7BIT, 0x48);
Serial.println("Sensor 5: ");
Serial.println(sen5);
const byte size = 5;
int senArray = {sen1, sen2, sen3, sen4, sen5};
Array array = Array(senArray,size);
maxa = array.getMax();
Serial.println("Max value of first array (senArray): ");
Serial.println(maxa);
for (int i=0; i<5; i++) {
if(maxa!=senArray*) continue;*
- else wantedpos = i + 1, Serial.println("The position of that number in the array: "), Serial.println(wantedpos);*
- }*
- const byte size2 = 4;*
- int senArray2[size2];*
- int j = 0;*
- for (int i=0; i<size2; i++) {*
_ if(i != wantedpos - 1) senArray2[j] = senArray*, j++;_
_ else continue;_
_ }_
_ Array array2= Array(senArray2,size2);_
_ max2 = array2.getMax();_
_ Serial.println("Max value of second array (senArray2): ");_
_ Serial.println(max2);_
_ for (int i=0; i<4; i++) {_
_ if(max2!=senArray2) continue;
else wantedpos2 = i + 1,Serial.println("The position of that number in the array: "), Serial.println(wantedpos2);
}
if (wantedpos = wantedpos2 = 0) {
//return 0;
Serial.println("It is zero and i am annoyed.");
}
else if (maxa - max2 < 75) { //75 is arbitrary number, in reality its probably closer to 10*
* dir = 0;
dir = (wantedpos + wantedpos2) - 1; //the final heading. -1 because 1 + 1 still has to equal 1 and 5 + 5 cant equal 10*
* Serial.println("maxa-max2 is less than 75: ");
Serial.println(dir); //Except, dir returns -1! Even though wantedpos and wantedpos2 both equal positive numbers that arent 0.
} else {
dir = 0;
dir = (wantedpos + wantedpos) - 1;
Serial.println("maxa-max2 is not less that 75: ");
Serial.println(dir);
}
Serial.println("help me");
delay(5000);
}[/quote]
So my big issue is that down the bottom, where it starts adding wantedpos and wantedpos2 together, no matter what they each equal, -1 is returned. Does anyone have any suggestions?
(referring to this bit)
> else if (maxa - max2 < 75) { //75 is arbitrary number, in reality its probably closer to 10*
> dir = 0;
> dir = (wantedpos + wantedpos2) - 1; //the final heading. -1 because 1 + 1 still has to equal 1 and 5 + 5 cant equal 10
> Serial.println("maxa-max2 is less than 75: ");
> Serial.println(dir); //Except, dir returns -1! Even though wantedpos and wantedpos2 both equal positive numbers that arent 0.
> }
god help me_