My code:
Servo myservo; // create servo object to control a servo
int val; // range-mapped value for servo control
const int pingPin = 4; // Parallax ping sensor's signal pin
const int numReadings = 5; // set higher to smooth more, also causes more latency
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int lastValue = 0;
void setup() {
// initialize serial communication:
Serial.begin(9600);
myservo.attach(A0); // attaches the servo on pin 9 to the servo object
//clear the smoothing value buffer
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
int fadeValue = 0;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
//smoothing code starts here
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = cm;//analogRead(inputPin);
// add the reading to the total:
total= total + readings[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:
average = total / numReadings;
//smoothing code ends here
//remap value range and move the servo
val = average;
val = map(val, 10, 40, 0, 179); // scale value to use it with the Tower Pro half turn analog servo (value between 0 and 180)
if(average < 25)
{myservo.write(val);} // sets the servo position according to the scaled value if within a certain distance
delay(10); //let the servo cool down, or something
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsT0oCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
Error Message:
In file included from ../inc/spark_wiring.h:29:0,
from ../inc/application.h:29,
from kingcobra.cpp:2:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
^
kingcobra.cpp:7:5: error: 'int index' redeclared as different kind of symbol
#line 1
^
In file included from /opt/gcc_arm/arm-none-eabi/include/stdlib.h:11:0,
from ../../core-common-lib/CC3000_Host_Driver/cc3000_common.h:43,
from ../../core-common-lib/SPARK_Firmware_Driver/inc/hw_config.h:35,
from ../inc/main.h:37,
from ../inc/spark_utilities.h:30,
from ../inc/spark_wiring.h:33,
from ../inc/application.h:29,
from kingcobra.cpp:2:
/opt/gcc_arm/arm-none-eabi/include/string.h:55:8: error: previous declaration of 'char* index(const char*, int)'
char *_EXFUN(index,(const char *, int));
^
kingcobra.cpp: In function 'void loop()':
kingcobra.cpp:43:35: error: 'pulseIn' was not declared in this scope
delayMicroseconds(5);
^
kingcobra.cpp:47:42: error: 'microsecondsToCentimeters' was not declared in this scope
// pulse whose duration is the time (in microseconds) from the sending
^
kingcobra.cpp:51:32: error: invalid types 'int [5][char*(const char*, int)]' for array subscript
^
kingcobra.cpp:53:17: error: invalid types 'int [5][char*(const char*, int)]' for array subscript
inches = microsecondsToInches(duration);
^
kingcobra.cpp:55:32: error: invalid types 'int [5][char*(const char*, int)]' for array subscript
^
kingcobra.cpp:57:19: warning: pointer to a function used in arithmetic [-Wpointer-arith]
// subtract the last reading:
^
kingcobra.cpp:57:9: error: assignment of function 'char* index(const char*, int)'
// subtract the last reading:
^
kingcobra.cpp:57:9: error: cannot convert 'char* (*)(const char*, int)' to 'char*(const char*, int)' in assignment
kingcobra.cpp:60:16: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
readings[index] = cm;//analogRead(inputPin);
^
kingcobra.cpp:62:11: error: assignment of function 'char* index(const char*, int)'
total= total + readings[index];
^
kingcobra.cpp:62:11: error: cannot convert 'int' to 'char*(const char*, int)' in assignment
kingcobra.cpp:28:7: warning: unused variable 'fadeValue' [-Wunused-variable]
}
^
make: *** [kingcobra.o] Error 1