Problem updating data counter | Arduino | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
Problem updating data counter
Avatar
pedretexksi
New Member
Forum Posts: 1
sp_UserOfflineSmall Offline
1
February 27, 2017 - 2:20 pm
sp_Permalink sp_Print

Hi everyone,
I´m new with IoT. I have an arduino connected to thingspeak and i want to send the number of times that a push button is activated.
The problem is that at the begining the arduino sends the first data to my chanel but then it stops.
If I use the fuction "delay" it sends the information but I can´t use this because the program is stopped during this seconds so I´m trying a timer library. I hope someone could help me to fix this. Thanks
Here is my code:

int BUTTON_PIN = 2;
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
unsigned long myChannelNumber = XXXXXXX;
const char * myWriteAPIKey = "XXXXXX";

#include <TimerOne.h>
#include "ThingSpeak.h"
#define USE_ETHERNET_SHIELD
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
EthernetClient client;

void setup() {

Timer1.initialize(180000000); // 18 s
Timer1.attachInterrupt(Thingspeak) ;
pinMode(BUTTON_PIN, INPUT_PULLUP);

//Serial.begin(9600);
Ethernet.begin(mac);
ThingSpeak.begin(client);
}

void loop() {

int buttonState = digitalRead(BUTTON_PIN);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
buttonPushCounter++;
//Serial.print("Counter: ");
//Serial.println(buttonPushCounter);

}
// Delay a little bit to avoid bouncing

delay(150);
// save the current state as the last state,for next time through the loop
noInterrupts(); // Suspende las interrupciones
lastButtonState = buttonState;
interrupts();

}

void Thingspeak() {
ThingSpeak.setField(1,buttonPushCounter);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
//ThingSpeak.writeField(myChannelNumber, 1, buttonPushCounter, myWriteAPIKey);
//Serial.print("Enviando datos ");
//Serial.println(buttonPushCounter);
}

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online: cstapels
25 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

yeseniara11

RomanFug

JamesPes

RobertBak

ScottSmiva

Amourhotte

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee