Created
December 2, 2011 21:39
-
-
Save parabuzzle/1424946 to your computer and use it in GitHub Desktop.
Simple arduino interface for LED manipulation based on monitoring.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// simple interface expecting data from serial | |
// provided by ardy.rb | |
// | |
// pin states: | |
// 48 = ok | |
// 49 = bad | |
// | |
// originated by mike heijmans | |
int ledPin = 13; // which pin do I control? | |
int state = 48; // what state do we start in? | |
byte incomingByte; // for incoming serial data | |
volatile long ticks = 0; // Start a counter tick variable for time keeping | |
int buttonPin = 2; // Pin for the button (on interrupt pin) | |
volatile int ack = 0; // setup an ack variable | |
volatile int buttonState = 0; // Initialize the button | |
// a function to blink the led once | |
void blinkLed(int pin, int rate) { | |
digitalWrite(pin, HIGH); | |
delay(rate); | |
digitalWrite(pin, LOW); | |
delay(rate); | |
} | |
// a function to activate the led on a pin | |
void ledOn(int pin){ | |
digitalWrite(pin, HIGH); | |
} | |
// a function to deactivate the led on a pin | |
void ledOff(int pin){ | |
digitalWrite(pin, LOW); | |
} | |
// a function to tick up the counter | |
void tickUp(int count){ | |
ticks=ticks+count; | |
} | |
// This is the interrupt routine function | |
void buttonPressed(){ | |
Serial.println("Button interrupt detected"); // print to serial that the interrupt has been activated | |
buttonState = digitalRead(buttonPin); // get the button state | |
if (buttonState == HIGH) { | |
// If circuit is closed do this stuff. | |
Serial.println("Ack button pressed."); // print to serial that the button is pressed | |
ticks = 0; // reset the timer | |
ack = 1; // set the ack variable | |
ledOn(ledPin); // activate the led to full on so we know we are in ack state. | |
delay(10); | |
} | |
} | |
// setup at startup | |
void setup() { | |
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output: | |
Serial.begin(9600); // begin the serial port listener | |
Serial.println("I am online\nWaiting for data..."); //print something to the serial port to express liveness | |
pinMode(buttonPin, INPUT); // Activate the button pin | |
attachInterrupt(0, buttonPressed, CHANGE); // attach the inturrpt routine for the button | |
} | |
//start the loop | |
void loop() { | |
// send data only when you receive data: | |
if (Serial.available() > 0) { | |
incomingByte = Serial.read(); | |
// print the data I got on to the serial port for debugging purposes | |
Serial.print("State received: "); | |
Serial.print(incomingByte, DEC); | |
Serial.print("\n"); | |
state = (incomingByte); // set the state int with what was sent | |
if (state == 49){ | |
// if I received 49, print a message to confirm the alert state (the blink comes later) | |
Serial.println("Alert condition detected: blinking LED at rate of 50ms"); | |
}else{ | |
// if I get the ok state, print a message to confirm for debugging | |
Serial.println("Everything is looking good"); | |
ledOff(ledPin); // also explicitly turn off the led for prosperity sake | |
} | |
} | |
if (state == 49){ | |
if (ack == 0){ | |
// If we are in an alert status and the ack state is not set, blink the led | |
blinkLed(ledPin, 50); | |
}else{ | |
// Ack state is set so we will figure out how long it has been set and do the needful. | |
if (ticks > 72000) { | |
// if the counter has gone over 72000 ticks (1 hour at 50ms tick rate) | |
ack = 0; // reset the ack flag | |
ticks = 0; // reset the counter | |
Serial.println("Acknowledgement timed out"); // print to serial that we have timed out | |
blinkLed(ledPin, 50); // blink the led | |
}else{ | |
// ack time has not expired | |
ledOn(ledPin); // Keep the led on | |
tickUp(1); // tick up by one | |
delay(50); // wait the tick rate (50ms) | |
Serial.println(ticks); // print what tick we are on. | |
} | |
} | |
} | |
if (state == 48){ | |
// If we are in OK state | |
delay(5000); // wait 5 seconds to prevent crashing because of the interrupt (don't know why... but this just worked) | |
ledOff(ledPin); // make sure the led is off | |
ack = 0; // make sure that ack is not set for the next go around the loop | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!ruby | |
############ | |
# this is the ardy script that uses a preloaded data file of HTML | |
# to send alerts to LED's on an arduino | |
# | |
# originated by mike heijmans | |
############ | |
#require needed libraries | |
require 'rubygems' | |
require 'serialport' | |
#params for serial port | |
port_str = "/dev/tty.usbserial-A900H0RW" #may be different for you | |
baud_rate = 9600 | |
data_bits = 8 | |
stop_bits = 1 | |
parity = SerialPort::NONE | |
#define the alert threshold | |
threshold = 2500 | |
#connect to arduino on serial port | |
sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity) | |
#start the loop | |
loop do | |
#read data from the data file (see get_data.sh for file population) | |
fh = File.open("data") | |
data = fh.read | |
fh.close | |
#parse the data | |
a = data.split("<tr />") | |
spool = 0 #set spool count to zero | |
a.each do |row| | |
if row.match(/web140/) #if the table row contains host data figure out the spool length | |
b = row.split("</td>") | |
spool = spool + b[1].gsub("\t<td style='text-align:center'>","").to_i #add the spool length of the host to the master spool variable | |
end | |
end | |
#check if spool length is bigger than the alert threshold | |
if spool >= threshold | |
#If it is too big, print an alert and send the flash bit to the arduino | |
puts "UHOH! the spooler is: #{spool}/#{threshold}" | |
sp.write(1) | |
else | |
#if everything is good, print status and send the reset bit | |
puts "ok: #{spool}/#{threshold}" | |
sp.write(0) | |
end | |
#take a nap | |
sleep 60 | |
end | |
#should never get to this bit of code... | |
puts "you shouldn't be here" | |
sp.close | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment