Skip to content

Instantly share code, notes, and snippets.

@stilldavid
Created January 12, 2012 17:08
Show Gist options
  • Save stilldavid/1601779 to your computer and use it in GitHub Desktop.
Save stilldavid/1601779 to your computer and use it in GitHub Desktop.
Free Day Geiger Counter
#include <Ethernet.h>
#include <SFEbarGraph.h>
#include <SPI.h>
// Default 'ino MAC address
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 2 };
int up = 8;
int down = 7;
int upstate = 0;
int downstate = 0;
// replace with your server
byte server[] = { 192, 168, 0, 1 };
EthernetClient client;
SFEbarGraph BG;
volatile int counter = 0;
int oldcount = 0;
int target = 8;
int multiplier = 2;
void setup() {
BG.begin(1, 9);
// Interrupt from the Geiger tube
attachInterrupt(0, detect, RISING);
Serial.begin(9600);
Ethernet.begin(mac, ip);
// set up buttons
pinMode(up, INPUT);
digitalWrite(up, HIGH);
pinMode(down, INPUT);
digitalWrite(down, HIGH);
delay(1000);
}
void loop() {
// read up button
if(LOW == digitalRead(up)) {
if(0 == upstate) {
target++;
redraw();
upstate = 1;
}
} else {
upstate = 0;
}
// read down button
if(LOW == digitalRead(down)) {
if(0 == downstate) {
target--;
redraw();
downstate = 1;
}
} else {
downstate = 0;
}
if(counter == oldcount) {
delay(1);
return;
}
oldcount = counter;
// handle wins
if(counter >= target) {
win();
counter = 0;
oldcount = 0;
return;
}
redraw();
}
// for the interrupt
void detect() {
counter++;
}
void redraw() {
// boundaries
if(target > (30 / multiplier))
target = (30 / multiplier);
if(target < 1)
target = 1;
BG.clear(); BG.send();
BG.barGraph(counter * multiplier, target * multiplier);
}
void win() {
Serial.println("WINNER");
for(int i = target * multiplier; i >= 0; i--) {
BG.barGraph(i, target * multiplier);
delay(35);
}
if ( ! client.connected()) {
Serial.println("connecting...");
if (client.connect(server, 5555)) {
Serial.println("connected");
} else {
Serial.println("connection failed");
}
}
// Print a 1 to the Ethernet server for every win
client.print("1");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment