Skip to content

Instantly share code, notes, and snippets.

@evanemolo
Created May 1, 2012 00:46
Show Gist options
  • Save evanemolo/2564028 to your computer and use it in GitHub Desktop.
Save evanemolo/2564028 to your computer and use it in GitHub Desktop.
arduino interval test code
//Twitter-Stepper Test
#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>
#include <Stepper.h>
#define STEPS 200
// interval declarations
long previousMillis = 0;
long interval = 10000; // interval to send GET request
// MAC address and server DNS:
byte mac[] = {
0x90, 0xA2, 0xDA, 0x00, 0x7E, 0xE9 };
char server[] = "twitknit-json-parsing.herokuapp.com";
EthernetClient client;
TextFinder finder (client);
Stepper stepper1(STEPS, 2, 3, 4, 5);
// declare global tweetCount var
long tweetCount = 0;
void setup() {
// set stepper speed
stepper1.setSpeed(60);
// start the serial library:
Serial.begin(9600);
}
void loop() {
// interval: check the difference between current time and time GET request sent
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
//save the last time GET request sent
previousMillis = currentMillis;
// start the ethernet connection:
Ethernet.begin(mac);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing:
while(true);
}
// give the ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// make HTTP GET request
client.println("GET / HTTP/1.0");
client.println("HOST: twitknit-json-parsing.herokuapp.com");
client.println();
}
else {
// if connection failed:
Serial.println("connection failed");
}
// if there are incoming bytes available from the server, read them and print them
if (client.available()) {
char c = client.read();
Serial.print(c);
// TextFinder code and stepper instructions:
if (finder.find("*") == true) { //find asterisk
long tweetCount = finder.getValue(); // get integer string (tweet count)
Serial.print(tweetCount);
Serial.println(" tweets");
// stepper instructions upon recieving tweets:
delay(500);
stepper1.step(tweetCount * -500);
}
}
// if the server is disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting");
client.stop();
// do nothing more
while(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment