I am trying to get data from an ultrasonic distance sensor for 5 seconds and then printing it out. I have one button attached to the red LED that starts recording data, that part looks good so far.
The problem is getting the data back from the array and printing it out.
Here is The Layout
Here is the Code
int btn_prev = LOW;
int btn_state = LOW;
int btn_pin = 8;
int pin = 7;
int printpin = 4;
int x=LOW;
int inches = 0;
int cm = 0;
int dist = 0;
int lastdist = 0;
int speed = 0;
const int rounds = (5+1);
int speedsave[rounds];
long readUltrasonicDistance(int pin){
pinMode(pin, OUTPUT); // Clear the trigger
digitalWrite(pin, LOW);
delayMicroseconds(2);
// Sets the pin on HIGH state for 10 micro seconds
digitalWrite(pin, HIGH);
delayMicroseconds(10);
digitalWrite(pin, LOW);
pinMode(pin, INPUT);
// Reads the pin, and returns the sound wave travel time in microseconds
return pulseIn(pin, HIGH);
}
void setup()
{
pinMode(pin, INPUT);
pinMode(printpin, INPUT);
pinMode (btn_pin, INPUT);
Serial.begin(9600);
}
void loop() {
btn_state = digitalRead(btn_pin);
if ( (btn_prev == LOW) && (btn_state == HIGH) ) {
// botton push starts round number of seconds to meassure speed
for (int i=0; i <= rounds ; i++){
lastdist = dist;
// measure the ping time in cm
cm = 0.01723 * readUltrasonicDistance(pin);
// convert to inches by dividing by 2.54
inches = (cm / 2.54);
dist = inches;
// calculate speed then print it
speed = (lastdist - dist);
Serial.print(speed);
Serial.print(" in/sec");
Serial.println();
speedsave = speed ;
- delay(1000); // Wait for 1 second(s)*
- }*
// Remember the previous button state for the next loop iteration - btn_prev = btn_state; *
*} *
*// print out array *
-
x = digitalRead (printpin);*
-
if ( (x == HIGH) ) {*
-
Serial.println(" Printing out Recorded Speeds... ");*
-
for (int j=1; j < rounds ; j++){ *
-
Serial.print (speedsave[j]);*
-
Serial.print(" in/sec");*
-
Serial.println();*
-
}*
-
x = LOW ;*
-
delay (400);*
-
}*
}