[Resolved] King Cobra Game Compile error! PLS HELP!

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

@5h177y, how are you compiling this code - IDE, CLI or locally? This will affect how your code is processed and some of the errors generated.

The function pulseIn() is not defined anywhere. I am not sure anyone has defined a library for this function. I am not sure why “index” is throwing an error but you may want to rename it to avoid the problem. The other errors seem to be falling out of the “index” error.

2 Likes

Hey All,

This is a small thing, I just noticed you have a function defined as microsecondsT0oCentimeters I’m guessing you meant microsecondsToCentimeters :smile:

Thanks,
David

1 Like

spark core’s web IDE
this is the code from:

Thx David, corrected but still the same error

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

Error: Could not compile. Please review your code.

I’ll just fix it for ya. :slight_smile:


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 idx = 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[idx];        
  // read from the sensor: 
  readings[idx] = cm;//analogRead(inputPin);
  // add the reading to the total:
  total= total + readings[idx];      
  // advance to the next position in the array: 
  idx = idx + 1;                   

  // if we're at the end of the array...
  if (idx >= numReadings)             
    // ...wrap around to the beginning:
    idx = 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 microsecondsToCentimeters(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;
}

/* 
 * pulseIn Function for the Spark Core - Version 0.1.1 (Beta)
 * Copyright (2014) Timothy Brown - See: LICENSE
 *
 * Due to the current timeout issues with Spark Cloud
 * this will return after 10 seconds, even if the
 * input pulse hasn't finished.
 *
 * Input: Trigger Pin, Trigger State
 * Output: Pulse Length in Microseconds (10uS to 10S)
 *
 */

unsigned long pulseIn(uint16_t pin, uint8_t state) {
    
    GPIO_TypeDef* portMask = (PIN_MAP[pin].gpio_peripheral); // Cache the target's peripheral mask to speed up the loops.
    uint16_t pinMask = (PIN_MAP[pin].gpio_pin); // Cache the target's GPIO pin mask to speed up the loops.
    unsigned long pulseCount = 0; // Initialize the pulseCount variable now to save time.
    unsigned long loopCount = 0; // Initialize the loopCount variable now to save time.
    unsigned long loopMax = 20000000; // Roughly just under 10 seconds timeout to maintain the Spark Cloud connection.
    
    // Wait for the pin to enter target state while keeping track of the timeout.
    while (GPIO_ReadInputDataBit(portMask, pinMask) != state) {
        if (loopCount++ == loopMax) {
            return 0;
        }
    }
    
    // Iterate the pulseCount variable each time through the loop to measure the pulse length; we also still keep track of the timeout.
    while (GPIO_ReadInputDataBit(portMask, pinMask) == state) {
        if (loopCount++ == loopMax) {
            return 0;
        }
        pulseCount++;
    }
    
    // Return the pulse time in microseconds by multiplying the pulseCount variable with the time it takes to run once through the loop.
    return pulseCount * 0.405; // Calculated the pulseCount++ loop to be about 0.405uS in length.
}

Thanks,
David

1 Like

Thanks alot David, there is no compile error, but the servo and sensor is not working…

Hi @5h177y,

You’ll have to dive in an examine your code, you still have other errors (like not setting pinmode correctly, etc).

Good luck!
David

Alright, I will, thanks a lot @Dave :smiley:

Hey, it looks like your original problem has been resolved so I’ve updated the topic to reflection the resolution.

However, If you feel that your problem still hasn’t been fixed feel free to continue positing in this thread!