Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

help me to understand this code

Status
Not open for further replies.

amr hamed

Newbie level 6
Joined
Mar 20, 2011
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,386
hey guys this a code 4 arduino microcontroller wired with 2 dc motors and ultrasonic sensor

const int numOfReadings = 10; // number of readings to take/ items in the array
int readings[numOfReadings]; // stores the distance readings in an array
int arrayIndex = 0; // arrayIndex of the current item in the array
int total = 0; // stores the cumlative total
int averageDistance = 0; // stores the average value
// setup pins and variables for SRF05 sonar device
int echoPin = 12; // SRF05 echo pin (digital 2)
int initPin = 13; // SRF05 trigger pin (digital 3)
unsigned long pulseTime = 0; // stores the pulse in Micro Seconds
unsigned long distance = 0; // variable for storing the distance (cm)
int motor1Pin1 = 3; // pin 2 on L293D
int motor1Pin2 = 4; // pin 7 on L293D
int enable1Pin = 9; // pin 1 on L293D
int motor2Pin1 = 5; // pin 10 on L293D
int motor2Pin2 = 6; // pin 15 on L293D
int enable2Pin = 10; // pin 9 on L293D
void setup() {
// set the motor pins as outputs:
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enable1Pin, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(enable2Pin, OUTPUT);
// set enablePins high so that motor can turn on:
digitalWrite(enable1Pin, HIGH);
digitalWrite(enable2Pin, HIGH);
pinMode(initPin, OUTPUT); // set init pin 3 as output
pinMode(echoPin, INPUT); // set echo pin 2 as input
// create array loop to iterate over every item in the array
for (int thisReading = 0; thisReading < numOfReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
digitalWrite(initPin, HIGH); // send 10 microsecond pulse
delayMicroseconds(10); // wait 10 microseconds before turning off
digitalWrite(initPin, LOW); // stop sending the pulse
pulseTime = pulseIn(echoPin, HIGH); // Look for a return pulse, it should be high as the pulse goes low-high-low
distance = pulseTime/58; // Distance = pulse time / 58 to convert to cm.
total= total - readings[arrayIndex]; // subtract the last distance
readings[arrayIndex] = distance; // add distance reading to array
total= total + readings[arrayIndex]; // add the reading to the total
arrayIndex = arrayIndex + 1; // go to the next item in the array
// At the end of the array (10 items) then start again
if (arrayIndex >= numOfReadings) {
arrayIndex = 0;
}
averageDistance = total / numOfReadings; // calculate the average distance
delay(10);
// check the average distance and move accordingly
if (averageDistance <= 10){
// go backwards
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
if (averageDistance <= 25 && averageDistance > 10) {
// turn
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
if (averageDistance > 25) {
// go forward
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
}

if any one can explain to me these codes jobs it will be nice
1-const int numOfReadings = 10;
2-int readings[numOfReadings];
3-int arrayIndex = 0;
4-int total = 0;
5-int averageDistance = 0;
6-total= total – readings[arrayIndex];
7-total= total + readings[arrayIndex];
8- readings[arrayIndex] = distance;
9-if (arrayIndex >= numOfReadings) {
arrayIndex = 0;

best regards
thanks

amr
 

if any one can explain to me these codes jobs it will be nice


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
const int numOfReadings = 10;       // numOfReadings declared as a constant equal to 10
int readings[numOfReadings];        // declares an array of 10 integers, readings[0], readings[1]...readings[9] 
int arrayIndex = 0;                 // declares an integer variable arrayIndex
int total = 0;                      // declares an integer variable total 
int averageDistance = 0;            // declares an integer averageDistance
total= total – readings[arrayIndex];  // total is assigned the value of total - the value of readings[arrayIndex]
total= total + readings[arrayIndex];    // total is assigned the value of total + the value of readings[arrayIndex]
readings[arrayIndex] = distance;        // readings[arrayIndex] is assigned the value of distance
if (arrayIndex >= numOfReadings) {      // if  arrayIndex greater or equal to numOfReadings then execute the code in the brackets {}
arrayIndex = 0;                         //  assign to arrayIndex the value 0



Alex
 

thnxxxxx alexan for ur help but can u give me ur e.mail or facebook , twitter acoounts plz .

best regards

amr
 

I don't use twitter or facebook, you can post here

Alex
 

ok i have some questions

why he choose 10 in this code : const int numOfReadings = 10
what is the meaning of this code : arrayIndex
what is the meaning of this code : readings[arrayIndex]
why he made a substraction here : total= total – readings[arrayIndex];
why he made add here : total= total + readings[arrayIndex];

sorry 4 annoyance

amr
 

Seems like the array is used to generate a moving average.
 

why he choose 10 in this code : const int numOfReadings = 10
the code uses this to declare an array of 10 integers, this array will be able to store 10 integer values,the programmer wanted to store 10 values for calculations.

what is the meaning of this code : arrayIndex
what is the meaning of this code : readings[arrayIndex]
The readings[arrayIndex] stores/returns the value of the array integers, when arrayIndex is 0 then the first integer of the array is used (readings[0]) , when arrayIndex is 4 then the fifth integer of the array is used (readings[4]),this can go all the way up to readings[9].
Instead of using ten different variable he uses an array of ten integers and he uses arrayIndex as an index to select one of them.

why he made a subtraction here : total= total – readings[arrayIndex];
why he made add here : total= total + readings[arrayIndex];
That is how he adds the new measurement to the total so that he can calculate the average at the end, he subtracts the old distance and then he adds the new one.
total= total - readings[arrayIndex]; // subtract the last distance (the previous value)
readings[arrayIndex] = distance; // add distance reading to array
total= total + readings[arrayIndex]; // add the reading to the total (add the new value)

Alex
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top