Hey all,
Pretty new to this and would love some help.
My ultimate goal is to increment a variable up by 1, and then publish that variable when an interrupt pin goes high.
I read through some tutorials and copy/pasted and modified some code to get something I would expect to work but am still getting errors.
Apologies if I’m missing something obvious (declaring variables properly etc). The only coding I’ve ever really done has been learning through squishing bits of example code together and trying to learn as I go.
Thanks in advance!
Brett
PS: How does everyone else in the forums get the copy pasted code to look like its in the IDE (colored etc)
int ledPin = 13; // LED is attached to digital pin 13
int x = 0; // variable to be updated by the interrupt
//variables to keep track of the timing of recent interrupts
unsigned long button_time = 0;
unsigned long last_button_time = 0;
void setup(){
attachInterrupt(D0, increment, RISING);
}
void loop() {
digitalWrite(ledPin, LOW);
delay(3000); //pretend to be doing something useful
}
// Interrupt service routine for interrupt 0
void increment() {
button_time = millis();
//check to see if increment() was called in the last 250 milliseconds
if (button_time - last_button_time > 250){
x++;
Spark.publish("x", &x, 60, PRIVATE);
digitalWrite(ledPin, HIGH);
last_button_time = button_time;
}
}
Not clear what’s the issue. There’s no publish or?..
Posting code in forum:
``` <--- insert this
//insert code here
``` <- insert this
Hey,
Thanks for the quick reply!
When I try compiling the code it gives me the error shown below. Unsure what it means.
Thanks,
Brett
In file included from ../inc/spark_wiring.h:29:0,
from ../inc/application.h:29,
from interupt_coffee_r1.cpp:3:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
^
interupt_coffee_r1.cpp: In function 'void increment()':
interupt_coffee_r1.cpp:36:35: error: no matching function for call to 'SparkClass::publish(const char [2], int*, int, Spark_Event_TypeDef)'
void increment() {
^
interupt_coffee_r1.cpp:36:35: note: candidates are:
In file included from ../inc/spark_wiring.h:33:0,
from ../inc/application.h:29,
from interupt_coffee_r1.cpp:3:
../inc/spark_utilities.h:110:14: note: static void SparkClass::publish(const char*)
static void publish(const char *eventName);
^
../inc/spark_utilities.h:110:14: note: candidate expects 1 argument, 4 provided
../inc/spark_utilities.h:111:14: note: static void SparkClass::publish(const char*, const char*)
static void publish(const char *eventName, const char *eventData);
^
../inc/spark_utilities.h:111:14: note: candidate expects 2 arguments, 4 provided
../inc/spark_utilities.h:112:14: note: static void SparkClass::publish(const char*, const char*, int)
static void publish(const char *eventName, const char *eventData, int ttl);
^
../inc/spark_utilities.h:112:14: note: candidate expects 3 arguments, 4 provided
../inc/spark_utilities.h:113:14: note: static void SparkClass::publish(const char*, const char*, int, Spark_Event_TypeDef)
static void publish(const char *eventName, const char *eventData, int ttl, Spark_Event_TypeDef eventType);
^
../inc/spark_utilities.h:113:14: note: no known conversion for argument 2 from 'int*' to 'const char*'
../inc/spark_utilities.h:114:14: note: static void SparkClass::publish(String)
static void publish(String eventName);
^
../inc/spark_utilities.h:114:14: note: candidate expects 1 argument, 4 provided
../inc/spark_utilities.h:115:14: note: static void SparkClass::publish(String, String)
static void publish(String eventName, String eventData);
^
../inc/spark_utilities.h:115:14: note: candidate expects 2 arguments, 4 provided
../inc/spark_utilities.h:116:14: note: static void SparkClass::publish(String, String, int)
static void publish(String eventName, String eventData, int ttl);
^
../inc/spark_utilities.h:116:14: note: candidate expects 3 arguments, 4 provided
../inc/spark_utilities.h:117:14: note: static void SparkClass::publish(String, String, int, Spark_Event_TypeDef)
static void publish(String eventName, String eventData, int ttl, Spark_Event_TypeDef eventType);
^
../inc/spark_utilities.h:117:14: note: no known conversion for argument 2 from 'int*' to 'String'
make: *** [interupt_coffee_r1.o] Error 1
@BrettA, it is always advisable to keep interrupt service routines “lite”. As such, doing a Spark.publish() in an ISR is not a good idea. Instead, set a flag that you check in loop() and publish from there.
I there a reason you want to have an interrupt-driven button versus a polled button? I use the clickButton() library and it let’s me set the debounce time plus allows me to use multi-clicks and long clicks on the same button. You may want to take a look. It is available in the web IDE libraries.
Change to this --> Spark.publish("x", String(x), 60, PRIVATE);
2 Likes
Thanks for the feedback. I was hoping to go the interrupt route because ultimately I’d like to modify the code and have it sleep until the interrupt happens, but I’m just taking baby steps to get there. Before I get to that point though I can definitely give the other method a try.
Best,
Brett
Awesome!
It compiles now. Haven’t actually run yet but will give it a go. Thanks for the help. Will keep you posted.
Best,
Brett
@BrettA, sounds good. However, the interrupt code may not work the way you expect. On the first defined (RISING or FALLING) edge of the sleep command, the processor will wake, regardless of your ISR code.
Hey guys,
Thanks so much. It works now!
@peekay123 as you suggested…it does run the increment function on both rising and falling edge even though I’d hoped it to only go on rising. As a first pass though–I’m just excited I can see the publishes working (I’ve hooked it up with IFTTT to just add a google drive spreadsheet line each time I publish). I really appreciate you guys taking the time to help me out here, and help me learn. If not for this forum I’d be sitting here stumped for hours and likely wouldn’t have made any further progress.
Thanks again!
Brett
2 Likes