View Full Version : Arduino Smoothing example failing to compile.

04-06-2014, 06:06 PM
Hello everyone,

This is my first post here, I hope this is the right place to bring up this issues.
I'm new Teensy adaptor and fairly new to c++ so please forgive any ignorance that I might show.

I'm trying to load the smoothing example that comes with arduino to Teensy 3.1, and I get the following error when compiling.
index(const char*, int)'
sketch_apr06a.ino: In function 'void loop()':
sketch_apr06a:48: error: invalid types 'int [10][char*(const char*, int)]' for array subscript
sketch_apr06a:50: error: invalid types 'int [10][char*(const char*, int)]' for array subscript
sketch_apr06a:52: error: invalid types 'int [10][char*(const char*, int)]' for array subscript
sketch_apr06a:54: error: assignment of function 'char* index(const char*, int)'
sketch_apr06a:54: error: cannot convert 'char* (*)(const char*, int)' to 'char*(const char*, int)' in assignment
sketch_apr06a:57: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
sketch_apr06a:59: error: assignment of function 'char* index(const char*, int)'
sketch_apr06a:59: error: cannot convert 'int' to 'char*(const char*, int)' in assignment

This program will be fundament I think for what I'm trying to do, which is collect biosignals using an EMG.
I will really appreciate if someone could give some advice as how to move forward.

Thanks in advanced.

04-06-2014, 06:16 PM
please post the relevant code.

04-06-2014, 06:18 PM
Sorry, Here it is



Reads repeatedly from an analog input, calculating a running average
and printing it to the computer. Keeps ten readings in an array and
continually averages them.

The circuit:
* Analog sensor (potentiometer will do) attached to analog input 0

Created 22 April 2007
By David A. Mellis <dam@mellis.org>
modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.


// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 10;

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 inputPin = A0;

void setup()
// initialize serial communication with computer:
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;

void loop() {
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = 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;
// send it to the computer as ASCII digits
delay(1); // delay in between reads for stability

04-06-2014, 06:24 PM
replace all "index" with "myIndex"
the var index is used by teensyduino

04-06-2014, 06:27 PM
Great!, Thank you for the quick reply!. I'm very much exited to use this platform in this and future projects. :)

04-06-2014, 07:21 PM
Yikes, hard to believe this issue hasn't come up before. Or maybe it has, but I just don't recall?

Looks like newlib defines index() and rindex() as string manipulation functions. That's not in avr-libc, but they are pretty standard in C libraries on traditional compilers.

The same problem happens with Arduino Due, so I've filed a bug report with Arduino.


Maybe the best thing for the moment would be to watch what Arduino does on this?

I'm leaning towards tweaking that header file to comment out the index() definition....