Problem understanding a certain source code.

Hey guys,

I'm new to robotics and arduino and I want to build a wireless accelerometer controlled RC Car.
It is a two axis accelerometer.
It uses two arduinos, one for transmission of the sensor readings and the other to recieve the readings and drive the car accordingly.
It uses 2 X-Bees for wireless communication.
I have pretty much understood the hardware and the logic behind it. But I still haven't understood the transmission code properly.
Would you guys please explain it to me? It would be of great help.
I am attaching the transmission code to this post.

Thanking you in advance,
SolidSnake31295

Tx.zip (1.24 KB)

Would you guys please explain it to me?

What part(s) of it don't you understand? It all looks quite straightforward to me.

If you don't understand some specific command, start here:

Sorry, I should have specified my queries earlier.
I've highlighted the parts which I havent understood and typed my query besides it.


const int xpin = A0; // x-axis of the accelerometer
const int ypin = A1; // y-axis

const int numReadings = 40;

int readings1[numReadings];
int readings2[numReadings];// the readings from the analog input
int index = 0;// the index of the current reading
int total1 = 0;
int total2 = 0; // the running total
int average1 = 0;
int average2 = 0;// the average

int inputPin1 = xpin;
int inputPin2 = ypin;

float result1 = 0;
float result2 = 0;
int result3 = 0;
float result4 = 0;
float result5 = 0;
int result6 = 0; Why has the declared these values?

int num0 = 0;
int num1 = 1;
int num2 = 2;
int num3 = 3;
int num4 = 4;
int num5 = 5;
int num6 = 6;
int num7 = 7;
int num8 = 8;

void setup(){
// initialize serial communication with computer:
Serial.begin(9600);

// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings1[thisReading] = 0;
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings2[thisReading] = 0;
}

void loop(){
// subtract the last reading:
total1= total1 - readings1[index];
total2= total2 - readings2[index];

// read from the sensor:
readings1[index] = analogRead(inputPin1);
readings2[index] = analogRead(inputPin2);

// add the reading to the total:
total1= total1 + readings1[index];
total2= total2 + readings2[index];

// advance to the next position in the array:
index = index + 1;

// if we're at the end of the array...
if (index >= numReadings)

// ...wrap around to the beginning:
index = 0;

// calculate the average:
average1 = total1 / numReadings;
average2 = total2 / numReadings;

// send it to the computer (as ASCII digits)
result1 = (average1 - 318)/(float)159;
if(result1>1) result1=1;
if(result1<-1) result1=-1;
result2 = asin(result1);
result3 = result2 * 57.32;

result4 = (average2 - 344)/(float)172;
if(result4>1) result4=1;
if(result4<-1) result4=-1;
result5 = asin(result4);
result6 = result5 * 57.32; I havent understood this part entirely. Why is he subtracting specific values like 318 and 344 from the averages and divide by 159 and 172 repectively? Why is he multiplying result3 and 5 with 57.32? and why is result2 or 5 = asin(result1 or 4)?

////////////Transmission//////////

/////////Forward//////////

if(result3 >= 11 && result3 <= 20){
Serial.print(num1);
}
Why specific values like 11 and 21? and why is he only using result3?
if(result3 >= 21 && result3 <= 30){
Serial.print(num2);
}
if(result3 >= 31 && result3 <= 40){
Serial.print(num3);
}
if(result3 >= 41 && result3 <= 50){
Serial.print(num4);
}
if(result3 >= 51 && result3 <= 60){
Serial.print(num5);
}
if(result3 >= 61 && result3 <= 70){
Serial.print(num6);
}
if(result3 >= 71 && result3 <= 80){
Serial.print(num7);
}
if(result3 >= 81 && result3 <= 90){
Serial.print(num8);
}

////////////Backward////////////

if(result3 >= -20 && result3 <= -11){
Serial.print("a");
}
if(result3 >= -30 && result3 <= -21){
Serial.print("b");
}
if(result3 >= -40 && result3 <= -31){
Serial.print("c");
}
if(result3 >= -50 && result3 <= -41){
Serial.print("d");
}
if(result3 >= -60 && result3 <= -51){
Serial.print("e");
}
if(result3 >= -70 && result3 <= -61){
Serial.print("f");
}
if(result3 >= -80 && result3 <= -71){
Serial.print("g");
}
if(result3 >= -90 && result3 <= -81){
Serial.print("h");
}

////////////////Left//////////////

if(result6 >= 11 && result6 <= 20){
Serial.print("i");
}

Why result6 now?

if(result6 >= 21 && result6 <= 30){
Serial.print("j");
}
if(result6 >= 31 && result6 <= 40){
Serial.print("k");
}
if(result6 >= 41 && result6 <= 50){
Serial.print("l");
}
if(result6 >= 51 && result6 <= 60){
Serial.print("m");
}
if(result6 >= 61 && result6 <= 70){
Serial.print("n");
}
if(result6 >= 71 && result6 <= 80){
Serial.print("o");
}
if(result6 >= 81 && result6 <= 90){
Serial.print("p");
}

////////////Right////////////////

if(result6 >= -20 && result6 <= -11){
Serial.print("q");
}
if(result6 >= -30 && result6 <= -21){
Serial.print("r");
}
if(result6 >= -40 && result6 <= -31){
Serial.print("s");
}
if(result6 >= -50 && result6 <= -41){
Serial.print("t");
}
if(result6 >= -60 && result6 <= -51){
Serial.print("u");
}
if(result6 >= -70 && result6 <= -61){
Serial.print("v");
}
if(result6 >= -80 && result6 <= -71){
Serial.print("w");
}
if(result6 >= -90 && result6 <= -81){
Serial.print("x");
}
}

Where did you got this code, did you asked your questions there?

Why has the declared these values?

To store intermediate results, think he/she/the author(s) copied 1 to 1 from datasheet

I havent understood this part entirely. Why is he subtracting specific values like 318 and 344 from the averages and divide by 159 and 172 repectively? Why is he multiplying result3 and 5 with 57.32? and why is result2 or 5 = asin(result1 or 4)?

Again, it looks like "datasheet code" in which formulas are presented .

I note that 318 = 2x159 and 344 = 2x 172. Think it is a divider for the time as a distancesensor has a bouncing signal - it measures twice the distance so that should be divided away. The other could be to convert milliseconds into centimeters or yards. The asin() I can't even guess why it is in it but maybe every serious robot should have one ;)

Why specific values like 11 and 21? and why is he only using result3?

This looks like a lookuptable code pattern, so again this looks like something coming from a datasheet, a table or a graph.
And yes it is code that can be optimized considerably, even not knowing what it does....

The fact the code uses 11 and 21 indicate the author(s) live in a country with the decimal system, - No I didn't study code anthropology :slight_smile:

The fact the numbers go from -90 to +90 indicates we are talking degrees /angles , which gives a meaning to the asin() function mentioned earlier as it is used to calculate angles. Now the magic number 57.32 makes sense to as it is almost (2*PI)/360 but only using 3.14 for PI instead of Math.PI or 3.14159265... so there is an unneeded error in that magic number.

So
result1 represents the sine of something sensorish
result2 converts that to radians
result3 converts that to degrees

same for result 4,5,6

Hopes this helps,

Thanks!! It helped a lot... :slight_smile: XD 8)

result1 = (average1 - 318)/(float)159;

Many MEMS accelerometers are ratiometric - the zero g point is at half the supply voltage, so a negative acceleration is represented by values less than half the supply, and positive by voltages above half the supply.
Different axes may have slightly different offsets.

Could that be the reason?

(Please use the # icon on the editor's toolbar when posting code)

Thanks for the info....

(Please use the # icon on the editor's toolbar when posting code)

I know, I simply didn't do that because I wanted to highlight the part which I didn't understand.

Anyways thanks for your help. :slight_smile: