Hello,
I'm new to any sort of coding, I have been using others peoples code to make things work generally which has been fine up till not and has allowed me very slowly to build up an idea of how the code works....
I have adjusted code I found for maxbotix ultrasonic sensors (thanks to the author) to make it work with 8 sensors chained together... Maxbotix state that it should be possible for the sensors to provide readings every 100ms... but at the moment with my beginners code it seems to be reading about every 1 second.... could this be because of my ineffcient coding?
Is there something really obvious as to why this is happening with my code? could someone possibly point me in the right direction?
//Feel free to use this code.
//Please be respectful by acknowledging the author in the code if you use or modify it.
//Author: Bruce Allen
//Date: 23/07/09
//Digital pin 7 for reading in the pulse width from the MaxSonar device.
//This variable is a constant because the pin will not change throughout execution of this code.
int pwPin1 = 2;
int pwPin2 = 3;
int pwPin3 = 4;
int pwPin4 = 5;
int pwPin5 = 6;
int pwPin6 = 7;
int pwPin7 = 8;
int pwPin8 = 9;
int triggerPin = 13; // output pin to start Ultrasound signals
//variables needed to store values
long pulse1, pulse2, pulse3, pulse4, pulse5, pulse6, pulse7, pulse8, cm1, cm2, cm3, cm4, cm5, cm6, cm7, cm8;
void setup() {
//This opens up a serial connection to shoot the results back to the PC console
Serial.begin(9600);
pinMode(pwPin1, INPUT);
pinMode(pwPin2, INPUT);
pinMode(pwPin3, INPUT);
pinMode(pwPin4, INPUT);
pinMode(pwPin5, INPUT);
pinMode(pwPin6, INPUT);
pinMode(pwPin7, INPUT);
pinMode(pwPin8, INPUT);
pinMode(triggerPin, OUTPUT); // set this pin to output
//give the sensors time to boot up
delay(250);
}
void loop() {
//Used to read in the pulse that is being sent by the MaxSonar device.
//Pulse Width representation with a scale factor of 147 uS per Inch.
digitalWrite(triggerPin, HIGH);
pulse1 = pulseIn(pwPin1, HIGH);
pulse2 = pulseIn(pwPin2, HIGH);
pulse3 = pulseIn(pwPin3, HIGH);
pulse4 = pulseIn(pwPin4, HIGH);
pulse5 = pulseIn(pwPin5, HIGH);
pulse6 = pulseIn(pwPin6, HIGH);
pulse7 = pulseIn(pwPin7, HIGH);
pulse8 = pulseIn(pwPin8, HIGH);
delayMicroseconds(30);
digitalWrite(triggerPin, LOW);
//58uS per cm
cm1 = pulse1/58;
cm2 = pulse2/58;
cm3 = pulse3/58;
cm4 = pulse4/58;
cm5 = pulse5/58;
cm6 = pulse6/58;
cm7 = pulse7/58;
cm8 = pulse8/58;
Serial.print(cm1);
Serial.print(" ");
Serial.print(cm2);
Serial.print(" ");
Serial.print(cm3);
Serial.print(" ");
Serial.print(cm4);
Serial.print(" ");
Serial.print(cm5);
Serial.print(" ");
Serial.print(cm6);
Serial.print(" ");
Serial.print(cm7);
Serial.print(" ");
Serial.print(cm8);
Serial.println();
delay(100);
}