« Added Accelerometer code | Test b2evolution -> twitter -> fb » |
Integrated 2nd Ultrasonic sensor
I finished soldering cables together and sorting them out so now i can finally sample both ultrasonic sensors at the same time and transmit their status over serial usb!
I also improved the code found on the site I mentioned some days before , by grouping together everything so I will be able to add functionallity without spaghetti code ( on a chip ) ..
I am also thinking of the protocol that I will create to perform functions on the arduino
I like this kind of things very much :o) ..!
When it will be ready I will also upload in in the robot github repo
On other news tommorow is my last class exam but I am not sure I will pass it if only my work here could be taken into account..
Code:
const int numOfReadings = 10; // number of readings to take/ items in the array | |
// setup pins and variables for SRF05 sonar device | |
| |
struct ultrasonic | |
{ | |
int echoPin; // SRF05 echo pin (digital 2) | |
int initPin; | |
unsigned int proximity; | |
int arrayIndex; // arrayIndex of the current item in the array | |
int total; // stores the cumlative total | |
int averageDistance; // stores the average value | |
int readings[numOfReadings]; // stores the distance readings in an array | |
}; | |
| |
| |
struct ultrasonic sensor1,sensor2; | |
| |
unsigned long pulseTime = 0; // stores the pulse in Micro Seconds | |
unsigned long distance = 0; // variable for storing the distance (cm) | |
unsigned int proximity=0; | |
| |
| |
| |
void setupUltrasonic(struct ultrasonic *ultr,int echopin,int initpin) | |
{ | |
pinMode(initpin, OUTPUT); // set init pin 3 as output | |
pinMode(echopin, INPUT); // set echo pin 2 as input | |
ultr->echoPin=echopin; | |
ultr->initPin=initpin; | |
for (int thisReading = 0; thisReading < numOfReadings; thisReading++) { ultr->readings[thisReading] = 0; } | |
ultr->proximity=0; | |
ultr->arrayIndex=0; // arrayIndex of the current item in the array | |
ultr->total=0; // stores the cumlative total | |
ultr->averageDistance=0; | |
} | |
| |
//setup | |
void setup() | |
{ | |
setupUltrasonic(&sensor1,2,3); | |
setupUltrasonic(&sensor2,4,5); | |
| |
// initialize the serial port, lets you view the | |
// distances being pinged if connected to computer | |
Serial.begin(9600); | |
} | |
// execute | |
| |
| |
int sampleUltrasonic(struct ultrasonic *ultr) | |
{ | |
digitalWrite(ultr->initPin, HIGH); // send 10 microsecond pulse | |
delayMicroseconds(10); // wait 10 microseconds before turning off | |
digitalWrite(ultr->initPin, LOW); // stop sending the pulse | |
pulseTime = pulseIn(ultr->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. | |
ultr->total= ultr->total - ultr->readings[ultr->arrayIndex]; // subtract the last distance | |
ultr->readings[ultr->arrayIndex] = distance; // add distance reading to array | |
ultr->total= ultr->total + ultr->readings[ultr->arrayIndex]; // add the reading to the total | |
| |
ultr->arrayIndex = ultr->arrayIndex + 1; // go to the next item in the array | |
| |
int proximity=0; | |
| |
if (ultr->arrayIndex >= numOfReadings) { ultr->arrayIndex = 0; } | |
ultr->averageDistance = ultr->total / numOfReadings; // calculate the average distance | |
// if the distance is less than 255cm then change the brightness of the LED | |
if (ultr->averageDistance < 255) { proximity = 255 - ultr->averageDistance; } // this means the smaller the distance the brighterthe LED. | |
| |
| |
return proximity; | |
} | |
| |
void loop() | |
{ | |
| |
| |
| |
//analogWrite(redLEDPin, redLEDValue); // Write current value to LED pins | |
//Serial.println(averageDistance, DEC); // print out the average distance to the debugger | |
int proximity = sampleUltrasonic(&sensor1); | |
Serial.println("Proximity 1 "); | |
Serial.println(proximity); | |
| |
proximity = sampleUltrasonic(&sensor2); | |
Serial.println("Proximity 2 "); | |
Serial.println(proximity); | |
| |
| |
delay(100); // wait 100 milli seconds before looping again | |
} |