hello,
i'm trying to use a function from another file. And apparently i'm not using "#include" correctly.
if i remove aprintf function code compiles fine.
The "aprintf" files are in the same as the "main" file directory
main file:
#include "aprintf.h"
//Push_Led.ino
//
// Turn LED only when the previous state of the push btn was "ON"
//
int PinLed = 2;
int PinBtn = 10;
int ON = 1 ;
int OFF = 0;
int state ;
char BLAH;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(PinLed, OUTPUT);
pinMode(PinBtn, INPUT_PULLUP);
state = digitalRead(PinBtn);
}
void TurnLed( int flag){
if(flag){
digitalWrite(PinLed, HIGH);
} else {
digitalWrite(PinLed, LOW);
}
}
void loop() {
BLAH = 'A';
ardprintf("Lets see - %s",BLAH);
}
aprintf.c:
int ardprintf(char s) {
};
and aprintf.h is :
#ifndef ARDPRINTF
#define ARDPRINTF
#define ARDBUFFER 16 //Buffer for storing intermediate strings. Performance may vary depending on size.
int ardprintf(char s)
#undef ARDBUFFER
#endif
[Stino - Start building "LED_turnonoff"...]
[ 3%] Creating /tmp/Stino_build/LED_turnonoff/LED_turnonoff.ino.cpp.o...
/home/az/Dropbox/work/Arduino/Sketches/Examples/LED_turnonoff/LED_turnonoff.ino: In function 'void loop()':
/home/az/Dropbox/work/Arduino/Sketches/Examples/LED_turnonoff/LED_turnonoff.ino:40:32: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
ardprintf("Lets see - %s",BLAH);
^
/home/az/Dropbox/work/Arduino/Sketches/Examples/LED_turnonoff/LED_turnonoff.ino:40:32: error: too many arguments to function 'int ardprintf(char)'
In file included from /home/azyman/Dropbox/work/Arduino/Sketches/Examples/LED_turnonoff/LED_turnonoff.ino:1:0:
/home/az/Dropbox/work/Arduino/Sketches/Examples/LED_turnonoff/aprintf.h:23:5: note: declared here
int ardprintf(char s) ;
^
[Stino - Exit with error code 1.]
And after changing the call to :
ardprintf('Y');
getting only:
/tmp/Stino_build/LED_turnonoff/LED_turnonoff.ino.cpp.o: In function `loop':
/home/az/Dropbox/work/Arduino/Sketches/Examples/LED_turnonoff/LED_turnonoff.ino:41: undefined reference to `ardprintf(char)'
collect2: error: ld returned 1 exit status
I moved h and c files over to library directory, since this is what stino "wants" after i click on "import library"....
it seemed to ignore these files if i put them into the same directory where the main file is...
//test.ino
#include "aprintf.h"
int led = 13;
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
}
void loop() {
led = ardprintf('Q');
}
in the ../test/libraries/aprintf/
aprint.h:
int ardprintf(char s) ;
aprint.c:
#include "aprintf.h"
int ardprintf(char s) {
return 10 ;
};
all right i've got this to work.
All files are in the same directory.
Should you choose to put it under library directory:
use the <>
create a subdirectory for you files
Add the library to list of libraries using your IDE.
//test.ino
#include "aaa.h"
int led = 13;
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
// led = ardprintf('Q');
for(int i=0; i< 10; i++){
Serial.println("DA");
delay(200);
}
}
aaa.cpp <- attention on the extention
#include "aaa.h"
int aaa(int a){
return a*a;
}
aaa.h
int aaa(int a);
The questions now i have :
a. Why am i instructed to include "Andruino.h", if it doesn't seem to be needed?
b. Why(where) do i need to use : #ifdef __cplusplus
extern "C" { #endif
// See Paul's post below
c. What is the correct way to use this functionality? ( the use of library)
d. What exactly is happening when i "add library" in the IDE ? Do i need to re-"add library" after i modified library files?