Show Posts
Pages: 1 ... 3 4 [5] 6 7 ... 20
61  Forum 2005-2010 (read only) / Syntax & Programs / Re: "undefined reference to" ...a function i on: October 21, 2010, 09:17:17 am
Not using the block declaration method results in errors:

Code:
extern "C" byte getPenStatus(){
  return pen_status;
}

Error msg: previous declaration of 'byte getPenStatus()' with 'C++' linkage

Quote
CONVERSION_06_MEGA.cpp: In function 'byte getPenStatus()':
CONVERSION_06_MEGA:21: error: previous declaration of 'byte getPenStatus()' with 'C++' linkage
CONVERSION_06_MEGA:74: error: conflicts with new declaration with 'C' linkage


The block method seems to work though so  smiley
62  Forum 2005-2010 (read only) / Syntax & Programs / Re: "undefined reference to" ...a function i on: October 21, 2010, 07:18:14 am
I also found that in order to call functions in my *sketch* from the .c code I had to declare  the function as "extern "C"" in the sketch - I've done that before - but didn't know @ the header declaration you posted. Looks like I have two-way communication happening now - so I can get back on track.

thanks!

Code:
extern "C"{
  void callSketch(int x){
   val2 += x;
   Serial.println(val2);
  }
}
63  Forum 2005-2010 (read only) / Syntax & Programs / Re: "undefined reference to" ...a function i on: October 20, 2010, 04:38:55 pm
YAY! That seems to work (error went away).

Are there any issues I should be aware of passing paramsfrom sketch > .c functions or returning values from .c function > sketch?

Thanks for taking the time!
64  Forum 2005-2010 (read only) / Syntax & Programs / "undefined reference to" ...a function i on: October 20, 2010, 03:06:04 pm
Sigh. I know there have been a lot of questions @ .h, .c, and .cpp files. I've read through the forum threads and also the description of the build process but I'm still struggling here. (I've tried lots of different things but nothing has worked).

Here's the set up:
-I want to use some open source c code.
-I have the code within my sketch folder and the .c and .h files show up as tabs.
-I have #includes (#include "line.h" - relative path) for each of the .h files at the top of my sketch
-My sketch compiles ok.

The problem is that I can not call any of the functions in the .c files without getting the "undefined reference" error. This occurs even if the function is declared in the .h file for corresponding .c file. Simply making the call below throws the error and prevents compilation

Is there some silver bullet here? The c code I am using compiles fine - I just can't reference it from my sketch. How do I open line of communication between my sketch and this #included code?

Thanks for any tips!

Code:
void loop(){
 plotter_init();
}

The error msg:
Quote
CONVERSION_05_MEGA.cpp.o: In function `loop':
CONVERSION_05_MEGA.cpp:37: undefined reference to `plotter_init()'

65  Forum 2005-2010 (read only) / Syntax & Programs / Re: another simple PSTR question: unsigned long format on: September 17, 2009, 01:45:09 pm
Ah, got it. Thanks. Was thinking it had something to do with PSTR. Doh!

This peek n' poke method I seem to be using in learning bits of C is probably not the way to go about it... ;-)
66  Forum 2005-2010 (read only) / Syntax & Programs / another simple PSTR question: unsigned long format on: September 17, 2009, 12:23:53 pm
Hi - still working through stuff with PSTR. I've got it to work with unsigned ints but I would like to use unsigned longs.

(background: I am using an array of type 'char' to store input from Serial.read(). When I reach the control char I want to pull the numeric representation out of the buffer of chars and use that as a number. It is possible that my data > that which an unsigned int can hold).

So right now I have this to generate the number:
Code:
X_BUFFER[Xindex++] = 0;
unsigned int x;
sscanf_P(X_BUFFER, PSTR("%u"), &x);

I searched around but can't find the format to indicate that an unsigned long should be used.

Oh - while I am here, I was trying to pass a length param (Xindex) to only read the num chars that had been stored from the most recent input and so be able to reuse the buffer without initializing it again. Couldn't figure out how to do that - but writing a 0 into the buffer with this line
Code:
X_BUFFER[Xindex++] = 0;
seems to work - so I get back a number of the correct length and without garbage chars added from past data sent. No idea why this works.

thanks for all the help!
--Roy
67  Forum 2005-2010 (read only) / Syntax & Programs / Re: "jumping the case label"? on: May 30, 2008, 07:53:55 pm
thanks. that makes sense. The "jumping the case label" msg didn't
68  Forum 2005-2010 (read only) / Syntax & Programs / "jumping the case label"? on: May 30, 2008, 09:35:55 am
Hi - small question here: I was working on a project yesterday where I was using a switch/case statement and attempting to define a local var in several of the cases - something like


Code:
case: 1
  // do some stuff
  break;

case 2:
  int p = resultOfFunctionCall();
  //do something with p
  break;

case 3:
  int p = resultOfOtherFunctionCall();
  //do something else with p

I was getting a compile error something like "jumping the case label" related to my local var. I resolved it by declaring the var inside the switch but outside any case. I am not returning the var at the end of the switch or anything like that so I wondered why the compiler would complain.

Is there some rule about not declaring local vars in a case statement?

tx

-Roy
69  Forum 2005-2010 (read only) / Syntax & Programs / Passing an array to a Class (Library) instance? on: February 12, 2010, 07:25:50 pm
Hopefully a simple question of syntax...

I have a class (Library) that I need two instances in my program but initialized with different values - arrays of non-consecutive numbers. I want to pass those arrays in when I init the instances. I am getting errors about 'incompatible types' int* and int.

What I am trying to do seems pretty basic. Is it just the wrong syntax? I can see some hack-around solutions but would like to understand how to do this right.

thanks! --Roy

In my sketch:
Code:
 int LC[] = {0,1,2,3,4,5,6,7,16,17,18,19};
  leftCircle.init('L',LC, 1, 25);

The .h file
Code:
#define CircleSmall_h
  
 #include "WProgram.h"
 
class CircleSmall
{
  public:
  int direction;
  int delayPeriod;
  int[] lightArray;
  CircleSmall();
  void init(int, int[], int, int);
};

#endif

The implementation (.cpp):
Code:
#include "CircleSmall.h"
 
CircleSmall::CircleSmall(){
//
}
 
void CircleSmall::init(int i, int L[] , int d, int p){
    id = i;    
    lightArray = L;
    direction = d;
    delayPeriod = p;
}
 



70  Forum 2005-2010 (read only) / Syntax & Programs / Re: "redefinition of class" error on: February 10, 2010, 01:05:57 am
Hey thanks for asking ;-)

I relaunched the Arduino IDE and the code compiled. Doh! - Not sure what is up with that. But it's late here and I'll take it for tonight.
71  Forum 2005-2010 (read only) / Syntax & Programs / "redefinition of class" error on: February 10, 2010, 12:49:54 am
Hi- I'm creating some custom Library classes for a project and am running into an error I can't track down. The error msg is:


error: redefinition of 'class CircleLarge'/Users/rise/Documents/Arduino/libraries/exciterLib/CircleLarge.h:13: error: previous definition of 'class CircleLarge'


But for the life of me, I can't see where I am doing this. I've made classes/libraries in Arduino before (and still have the scars ;-), and run into assorted scope and type definition problems but was able to sort them out. Not so this time.

Anyway - I'll post the code below. I've stripped out the function content to shorten it. The .h + .cpp files are in the Libraries folder in my Arduino user directory. The error is getting flagged on line 13 of CircleLarge.h.

thanks for any hints! -- Roy

The sketch:
Code:
#include <Light.h>
#include <CircleLarge.h>
#include <CircleSmall.h>
//----------------------------------------
Light spriteArray[64];

CircleSmall leftCircle;
CircleLarge centerCircle;
CircleSmall rightCircle;

void setup(){

  setUpCircles();
  leftCircle.init('L',0, 11, 1, 25);
  centerCircle.init(12, 51, 1, 25);
  rightCircle .init('R',52, 63, -1, 25);
}

 void loop(){
  long m = millis();

  // leftCircle.update(m);
  //  centerCircle.update(m);
  //  rightCircle.update(m);

  // draw everything :: deal with LEDs
  for (int i = 0; i < 64; i++){
    spriteArray[i].display(m);
  }
}

void setUpCircles(){
  // **********************
  // Left: 12, Center: 40, Right: 12
  // draws CCW
  //  Left & Center start at 3 o'clock - Right starts at 9 o'clock
  // loaded into array in order: L/C/R
  // ****************************
  int num;
  int offset;
  int index;

  num = 12;
  offset = 0;
  index = 0;
  // LEFT CIRCLE
  for (int i=0; i<12; i++) {
    index = i + offset;
    spriteArray[i].init(i, index);
  }

  // CENTER CIRCLE
  num = 40;
  offset = 12;
  for (int i=0; i<40; i++) {
    index = i + offset;
    spriteArray[i].init(i, index);
  }

  // RIGHT CIRCLE
  num = 12;
  offset = 52;
  for (int i=0; i<12; i++) {
    index = i + offset;
    spriteArray[i].init(i, index);
  }
}

CircleLarge.h
Code:
#ifndef CircleLarge_h
#define CircleLarge_h
 
//#include "wiring.h"
 #include "WProgram.h"
 
class CircleLarge
{ //// THIS IS LINE 13 WHERE THE ERROR IS OCCURRING

   public:
      int state;
      int sIndex;
      int eIndex;
      int cIndex;
      int leftTrigger;
      int rightTrigger;
      long nextUpdate;
      int delayPeriod;
      int currentDelay;
      int direction;
 
      int arrayLength;
      char shiftDirection;
      
      long lastPulseLeft, lastPulseRight;
      int leftPulseStrength, rightPulseStrength;

      char leftPulseShiftDirection;
      char rightPulseShiftDirection;
      int lightArray[40][3];
      long leftTimeArray[];
      long rightTimeArray[];
  

      CircleLarge();
      void init(int s, int e, int d, int p);
      void update(long m);
      long* shiftArray(long* a, char d);
      void pulseLeft(long m, char direction);
      void pulseRight(long m);
      int getLeftPulseStength();
      int getRightPulseStength();
      void clearLeftPulseStrength();
      void clearRightPulseStrength();
      void setRightPulseShiftDirection(char d);
      void setLeftPulseShiftDirection(char d);
      void pulse(int side);
      void setDirection(int d);
  
};

#endif

CircleLarge.cpp

Code:
#include "Light.h"
#include "CircleLarge.h"

extern Light spriteArray[64];

CircleLarge::CircleLarge(){
   //
}
void CircleLarge::init(int s, int e, int d, int p){
}
void CircleLarge::update(long m){
 }
long* CircleLarge::shiftArray(long* a, char d){
}
void CircleLarge::pulseLeft(long m, char direction){    
}
void CircleLarge::pulseRight(long m){    
}
int CircleLarge::getLeftPulseStength(){
    return leftPulseStrength;
}
int CircleLarge::getRightPulseStength(){
    return rightPulseStrength;
}
void CircleLarge::clearLeftPulseStrength(){
    leftPulseStrength = 0;
}
void CircleLarge::clearRightPulseStrength(){
    rightPulseStrength = 0;
 }
void CircleLarge::setRightPulseShiftDirection(char d){
    rightPulseShiftDirection = d;
 }
 void CircleLarge::setLeftPulseShiftDirection(char d){
    leftPulseShiftDirection = d;
}
void CircleLarge::pulse(int side){
 }
void CircleLarge::setDirection(int d){
    direction = d;
}
72  Forum 2005-2010 (read only) / Syntax & Programs / Re: setting value of multi-dimension array? on: January 10, 2010, 02:00:31 pm
ok - thanks!
73  Forum 2005-2010 (read only) / Syntax & Programs / setting value of multi-dimension array? on: January 10, 2010, 01:35:03 pm
Hi - was thinking I could set the value of a mult-dimensional array in one go - something like this:

Code:
int test[5][3];
 
void setup(){
test[1] = {1,1,1};
}

But that throws the error error: expected primary-expression before '{' token

Tried some variations:
test[1] = [1,1,1]; etc. with same error. The value of test[1] is an array of 3 ints so I am not getting why I can't set it directly. I know I can do this:

Code:
 test[1][0] = 1;
  test[1][1] = 1;
  test[1][2] = 1;

But that seems very clumsy. Am I missing something or is this just not possible?

tx!
  
74  Forum 2005-2010 (read only) / Syntax & Programs / choreographic programming approaches? on: January 06, 2010, 02:54:14 pm
Hi - I do a lot of art projects using LEDs - sometimes in grids but more often in linear shapes described by the LEDs (intersecting circles, crosses, etc.). I always struggle with what I guess could be called the 'choreography' of programming these pieces: setting up the sequences and timing of movements (i.e. triggering the LEDs in particular animation sequences and patterns, interaction between these virtual 'shapes' and so on).

Most of my stuff has some generative and/or random aspect - I set up rules, etc. and the sequences never repeat but have some structure or development over time.

I know how to use objects/classes, and programming elements like state machines, LUTs, etc. but I am wondering how to go about setting up a programming structure/system that I can build on.

Very general question, I realize but maybe someone has suggestions or examples I might look at to get some ideas on how to approach coding such an animation system.

tx!
75  Forum 2005-2010 (read only) / Syntax & Programs / Re: reinitializing an array? on: December 10, 2009, 07:48:32 am
thanks for the comments. I guess it makes sense that my line: collector[4] doesn't do anything since there isn't any assignment but it didn't throw an error either. I did try redeclaring the array with null values

Code:
collector = {'/0','/0','/0','/0'}
but that didn't work. Hadn't occurred to me to step a null char through the array. Anyway, I was confused @ there not being a way to just blast out the existing values and reset the array to all null values.

fwiw - I'm using that array and atoi (getColor() function) to read in numbers in the range of 0..1024. Tried using bitshifting an bit OR but couldn't get it to work.

Code:
//// Serial Test: receiving large numbers
 
int RGB[3];
char state = 'i';

void setup() {
  Serial.begin(9600);
  Serial.println("ok");
}
 
void loop(){

  switch (state){
  case 'i':
    getColor();
    break;

  case 'c':
    state = 'i';

    /// use new color
    Serial.print(RGB[0]);
    Serial.print("  ");
    Serial.print(RGB[1]);
    Serial.print("  ");
    Serial.print(RGB[2]);
    Serial.println("  ");

    break;
  }
}


int getColor(){
  if  (Serial.available()){
    int position = 0;
    char collector[4];
    char c;
    int counter = 0;
    while(true){
      if (Serial.available()){
        c = Serial.read();
        if (c == ',' || c =='*'){
          int n = atoi(collector);   //convert to number

            Serial.println(n);
          RGB[position] = n;         //store
          position++;                //increment array index

          if (c == '*') {
            state = 'c';
            break;
          }
          for(int i = 0; i < 4; i++){
            collector[i] = ' ';
          }

          counter = 0;               //reset char array index
        }
        else{
          collector[counter] = c;
          counter++;
        }
      }
    }
  }
}
Pages: 1 ... 3 4 [5] 6 7 ... 20