Problem putting funct in ext c file and pointers

Hello,

I have been trying to get this for many hours tonight (about 4 now). I have a function that looks like this

void powercell(int currentPowercell)
{
  switch(currentPowercell)
  {
  case 0:
    POWERCELL0_ON;
    break;
  case 1:
    POWERCELL1_ON;
    break;
  case 2:
    POWERCELL2_ON;
    break;
   }
   currentPowercell++;
}

Now I have tried to make it a pointer and that just causes errors. I have tried to pass by reference (or use the &, if that is what it is called) and that causes errors as well.

I have tried to set up the prototype in the code as voide powercell(*int) and void powercell(&int), but it is all to no avail. I have this function and others that I am fighting with in an external c file. And no I cannot simply use a return because most functions change 2 or 3 values, this is just the simplest so I thought I would start there.

Any help is greatly appreciated. I am at a loss as to what I am doing wrong.

First up.. Post all your code. It'll make it easier to identify the problem..

Secondly. What's the purpose of "POWERCELL2_ON;" .... You can't do that.

If it's an enumeration or defined int then you need to assign it to something.

varx = POWERCELL2_ON;

or POWERCELL2_ON();

Post your compiler error...

The code is very long (over 1000). It was all working before I decided to clean it up and move things to external files I will post portions of it.

led.h

#ifndef LED_H
#define LED_H

//define pins
#define  SER_OUT 9
#define  SRCK 7
#define  RCK  8
#define  powerOnPin 6
#define  wandOnPin 16
#define  safetyPin 17
#define  triggerPin 18

unsigned char ledValues[6];  // number of shift registers


//define the leds
#define  POWERCELL0_ON   ledValues[0] = ledValues[0] | 0x01; TPIC6B959N_write();
#define  POWERCELL1_ON   ledValues[0] = ledValues[0] | 0x02; TPIC6B959N_write();

#define  POWERCELL0_OFF  ledValues[0] = ledValues[0] & 0xfe; TPIC6B959N_write();
#define  POWERCELL1_OFF  ledValues[0] = ledValues[0] & 0xfd; TPIC6B959N_write();
#endif

sketch

#include <AF_Wave.h>
#include <avr/pgmspace.h>
#include "led.h"
#include "sound.h"
#include "test.c"
int currentPowercell; //current led that it is on
long startPowercell;   //the last time that it was fired

void TPIC6B959N_write()
{
  unsigned char i, numOfBytes;
  unsigned char loopLedValues;
  digitalWrite(SRCK, LOW);
  digitalWrite(RCK, LOW);
  /* shift out the bits */
  for (numOfBytes = sizeof(ledValues);numOfBytes > 0;--numOfBytes) {
    for (i = 8;i > 0;--i) {
      loopLedValues = (ledValues[numOfBytes - 1] >> (i - 1));
      loopLedValues = (loopLedValues & 1);
      digitalWrite(SER_OUT, loopLedValues);
      digitalWrite(SRCK, HIGH);
      digitalWrite(SRCK, LOW);
    }
  }
  digitalWrite(RCK, HIGH);      // enable outputs
  digitalWrite(RCK, LOW);
}

void powercell(int*);
void loop()
{
    int powercellSpeed = analogRead(powercellSpeedPin);  //how fast the powercell moves
    powercellSpeed=max(powercellSpeed, 10);
    if (millis()>=(startPowercell+powercellSpeed))
    {
      startPowercell=millis();
      powercell(&currentPowercell);
    }
}

test.c

#include "led.h"

void TPIC6B959N_write();

void powercell(int *currentPC)
{
  switch(*currentPC)
  {
  case 0:
    POWERCELL0_ON;
    break;
  case 1:
    POWERCELL1_ON;
    break;
  case 14:
    POWERCELL14_ON;
    *currentPC=-1;
    POWERCELL0_OFF;
    POWERCELL1_OFF;]
    break;
  }
  *currentPC++;
}

So there you have it. Like I said, the code was working before I broke it up into the external c file. I realize that there are portions of the required arduino structure missing. This is only a portion of the code, all of the parts that I feel need to be seen in order to troubleshoot this issue. I cannot get it to retain the value of currentPowercell (currentPC in the function). I have tried passing it as a pointer and byreference. I do not get an error with the current code, it just doesnt work. The first version of this code was a quick and dirty hack to prove that it could be done. I am looking to make this version a little better than that, hence the breaking up of things. The first version used global variables since I could not get passing arguments to work. I am on arduino 0013Alpha because I am using adafruits waveshield and am not sure if the library works with the newest arduino.

Thanks

-Syco54645

Try putting your .h file contents inside "C" designators:

#ifdef __cplusplus
extern "C"{
#endif

 //  Bulk of .h file

#ifdef __cplusplus
} // extern "C"
#endif

Otherwise function definitions in particular, and perhaps data definitions, get tagged with additional bits of symbolness so as to be able to handle overloading and such. Or something.

I will give that a try this evening when I get home. Hopefully this fixes it as I just want to get this rolled out for everyone.

1000 Lines long! Wont you start struggling to find eeprom for a program that size!

Skynet here we come.

Try zipping it up if the problem persists, we can try and figure it out

woops i meant 100 lines. i did not want to make my post annoyingly huge. true line count is about 300 some i think. problem is persisting. i can zip it up if need be. i am just not sure that I am doing it right. Truth be told I have not touched c in a number of years, 7 maybe? I have been straight web development for a while now, well with some perl for when I needed something quick and dirty. I can zip it up when I get home and see what can be figured out.

Edit: Well, my code is written to drive a circuit that I have developed using 6 tpic6b595 chips. So people could look at my code but that is about it, they would not be able to test it, but the more eyes the better.

OK, so I did some debugging. I am using reference variables and have the function in the sketch and it works fine. When moving the function to the included c file it throws up with this error "error: expected ';', ',' or ')' before '&' token". My function is defined as "void powercell(int& currentPC)"

Zip it all up my good man...

Problem has been solved via the ##c channel on freenode. Here is how I fixed it for future people.

Here is the prototype for the function

void powercell(int*);

Here is how the function was called

powercell(¤tPowercell);

Here is the function in the test.c file that is included

void powercell(int *currentPC)
{
  switch(*currentPC)
  {
  case 0:
    POWERCELL0_ON;
    break;
  case 1:
    POWERCELL1_ON;
    break;
  case 15:
    *currentPC=1;
    POWERCELL0_OFF;
    POWERCELL1_OFF;
    break;
  }
  *currentPC+=1;
}

All of this was what I needed to cause the function to work without global variables. Thanks for helping.

-Syco54645

ps my code line count is

sketch: 519 sound: 72 test.c: 118

so my original statement of 1000 lines, even though I meant to type 100, was almost correct. There is still more code to be added as well. Not having an issue fitting on the atmega so far. Hopefully it will continue like that.