# Help with LittleBits / CloudBits Incremental Counter

Hi All - I’m using LittleBits (arduino Leonardo) , CloudBits , and IFTTT to setup a tweet counter. My pseudo logic goes like this : If there are 10 tweets, light up 1 light, If there are 20 tweets, light up 2 lights, etc. up to 5 lights.

The way IFTTT plus CloudBits works is pretty cool - just like this :

• when there is a tweet w a certain hashtag, IFTTT sends a HIGH signal to my CloudBits.
• the CloudBits module is HIGH (the variable value is > 500) for 1 second
• after that 1 second, the cloudBitsValue VAR fluctuates between 1 and 10.

In my code, I’ve got a button counter working really well, and my lights are going on with a manual button with no issues. I’ve also got serial readings coming in nicely.

But I can’t for the life of me figure out the logic of increasing my counter variable by only 1 when this cloudbits piece is ON. The IFTTT requires the bit to be on for at least 1 second, so I can’t quite just

if (cloudBits == HIGH) {
tweetCounter++ ;
}

But I’m looking to do something similar , I believe.

Halp?

Code is all below

``````/*
littleBits Arduino Counter
Richard Born
May 7, 2014
*/

const int resetButton = A0; // this button will reset counter to zero
const int countButton = 0; // this button counts up and down presses of the count button
const int cloudBitsPin = A1; // this sets up the CLOUDBITS value as an INPUT
const int goGordonGoPin = 1; // this is the wireLess Transmitter / TEST LED OutPut
const int barGraph = 9; // BARGRAPH OUTPUT

// The following array converts PWM values to integer values from 0 to 99
// For example: numbers[0] = 0; numbers[1] = 3; numbers[2] = 6; numbers[3] = 8, etc.
// This allows us to display a number on the number module based upon the corresponding PWM value
int numbers [] = {0,3,6,8,10,13,15,18,20,23,26,28,30,33,35,38,40,43,45,48,50,53,55,58,61,
64,66,69,71,74,76,78,81,84,86,89,91,94,96,99,102,104,106,108,111,114,116,
119,121,124,127,129,132,134,136,139,141,143,146,149,151,154,156,159,160,163,
166,169,171,173,176,179,181,184,186,188,190,193,196,199,202,205,208,210,212,
214,217,218,221,224,227,230,232,235,237,239,242,245,247,252,255};

int resetButtonState = 0;        // current state of the reset button
int countButtonState = 0;        // current state of the count button
int countOldButtonState = 0;     // previous state of the count button
int count = 0; // counts the number of presses of the count button
int upsAndDowns = 0; // counts presses and releases of the count button
int barGraphValue = 0;    // variable to hold the value of the analog output on D9
int cloudBitsValue = 0; // variable to hold the value of CloudBits Data input
int goGordonGoState = 0; // variable to hold value of the GoGordonGo transmitter on D1 , set to 0 (OFF)
int tweetCounter = 0; // variable to hold how many tweets came in

// NO TWEETS YET, initialize
// A TWEET CAME IN, plus 1 the counter just once
// TWEETS ARE DONE, WAITING, don't do anything to the tweetCounter

void setup() {
pinMode(resetButton, INPUT);     // sets pin to INPUT mode
pinMode(countButton, INPUT);     // sets pin to INPUT mode
pinMode(cloudBitsPin, INPUT); // sets CloudBits to INPUT mode
pinMode(barGraph, OUTPUT);    // sets pin to OUTPUT mode
pinMode(goGordonGoPin, OUTPUT);    // sets GoGordonGo pin to OUTPUT mode
Serial.begin(9600);      // open the serial port at 9600 bps for Monitoring outputs on Laptop

}

void loop() {

resetButtonState = digitalRead(resetButton); // get the state of the reset button
countButtonState = digitalRead(countButton); // get the state of the counter button

// barGraphValue = analogRead(cloudBitsValue); ??? sets the value of the BarGraph to the TweetCount
cloudBitsValue = analogRead(cloudBitsPin); // --> THIS IS FOR TESTING JUST ONE TWEET COMING IN
//SET ANALOG OUT TO COUNTVALUE

// SETS EVERYTHING TO ZERO
if (resetButtonState == HIGH) {
// reset upDown counter to zero and Turn Off Lights
upsAndDowns = 0;
count = 0; //resets Count
tweetCounter = 0; // resets TweetCounter
digitalWrite(goGordonGoPin, LOW); //turns off BarGraph
analogWrite(barGraph, 0); //turns off GoGordonGo
}

// WHEN BUTTON IS PRESSED
if ((countButtonState == HIGH && countOldButtonState == LOW) || (countButtonState == LOW && countOldButtonState == HIGH)) {
// counter button has been pressed down or has been released
upsAndDowns += 1;
count = upsAndDowns/2; // actual count is half the ups and downs
if (count == 10000) {
upsAndDowns = 0;
count = 0;
}
countOldButtonState = countButtonState; // saves the old state of the count button
}

// WHEN THERE IS A TWEET, INCREASE THE VALUE OF CLOUDBITS -- NOT WORKING - HALP????

//LIGHT THINGS UP from the CLOUDBIT --> TESTING FROM BROWSER
analogWrite(barGraph, cloudBitsValue); //--> ALSO TESTING FOR CLOUDBITS INPUT TWEET

if (tweetCounter == 2) {
analogWrite(barGraph, 815); // Green 1st Spot
}
if (tweetCounter == 4) {
analogWrite(barGraph, 611); // GREEN 2nd Spot
}
if (tweetCounter == 6) {
analogWrite(barGraph, 407); // GREEN 3rd Spot
}
if (tweetCounter == 8) {
analogWrite(barGraph, 203); // YELLOW 4th Spot
}
if (tweetCounter >= 10) {
digitalWrite(goGordonGoPin, HIGH);
analogWrite(barGraph, 1023); // Red 5th Spot , Full on GoGordonGo Mode
}

// LIGHT THINGS UP VIA THE BUTTON
// !!! I WILL DO A RANGE of VALUES - RIGHT NOW - THIS LOOKS FOR THE GoGordonGo COUNT of Go! , set to 10 right now
if (count == 2) {
analogWrite(barGraph, 815); // Green 1st Spot
}
if (count == 4) {
analogWrite(barGraph, 611); // GREEN 2nd Spot
}
if (count == 6) {
analogWrite(barGraph, 407); // GREEN 3rd Spot
}
if (count == 8) {
analogWrite(barGraph, 203); // YELLOW 4th Spot
}
if (count >= 10) {
digitalWrite(goGordonGoPin, HIGH);
analogWrite(barGraph, 1023); // Red 5th Spot , Full on GoGordonGo Mode
}
Serial.print( "The ButtonCount is:" + String(count) + " "); // print the variable "COUNT"
Serial.print( "The TweetCount is:" + String(tweetCounter) + " "); // print the variable "COUNT"
Serial.println( "The CloudBit Value is:" + String(cloudBitsValue) + " "); // print the variable "COUNT"

delay(20); // debounce time for buttons
}
``````

Counter.ino (5.22 KB)

Here are some of the bits I'm using for reference : Quick, start! A quickstart project for LittleBits Arduino: a littleBits Project by mikimer

This is the LittleBit Arduino : http://littlebits.cc/bits/arduino
And this is the CloudBit : Snap Circuits | Shop littleBits: Bits, Dimmers, ∓ More | Sphero

Also - this seems somewhat relevant : https://www.arduino.cc/en/Tutorial/WhileStatementConditional?from=Tutorial.WhileLoop