Skip to content

Instantly share code, notes, and snippets.

@johnschimmel
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnschimmel/f5371d8ad5b8bc00528c to your computer and use it in GitHub Desktop.
Save johnschimmel/f5371d8ad5b8bc00528c to your computer and use it in GitHub Desktop.
3 Relays + Serial input
/*
Pins, 13-11 connected to relays
serial input 1-3 to toggle relay on for 2 seconds
from Tom Igoe's RGB Serial example
This example code is in the public domain.
*/
// pins for the LEDs:
const int pinA = 13;
const int pinB = 12;
const int pinC = 11;
long pinATime = 0;
long pinBTime = 0;
long pinCTime = 0;
int interval = 2000;
void setup() {
// initialize serial:
Serial.begin(9600);
// make the pins outputs:
pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinC, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - pinATime > interval) {
digitalWrite(pinA, LOW);
}
if(currentMillis - pinBTime > interval) {
digitalWrite(pinB, LOW);
}
if(currentMillis - pinCTime > interval) {
digitalWrite(pinC, LOW);
}
// if there's any serial available, read it:
while (Serial.available() > 0) {
int number = Serial.parseInt();
if (Serial.read() == '\n') {
Serial.println(number);
if (number == 1) {
digitalWrite(pinA, HIGH);
pinATime=currentMillis;
}
if (number == 2) {
digitalWrite(pinB, HIGH);
pinBTime=currentMillis;
}
if (number == 3) {
digitalWrite(pinC, HIGH);
pinCTime=currentMillis;
}
}
}
}
/**
* Simple Write.
*
* Check if the mouse is over a rectangle and writes the status to the serial port.
* This example works with the Wiring / Arduino program that follows below.
*/
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
void setup()
{
size(200, 200);
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
println(Serial.list());
String portName = Serial.list()[4];
myPort = new Serial(this, portName, 9600);
}
void draw() {
background(255);
if (mouseOverRect() == true) { // If mouse is over square,
fill(204); // change color and
myPort.write("1\n"); // send an H to indicate mouse is over square
// myPort.write(11);
}
// else { // If mouse is not over square,
// fill(0); // change color and
// myPort.write(2); // send an L otherwise
// }
rect(50, 50, 100, 100); // Draw a square
}
boolean mouseOverRect() { // Test if mouse is over square
return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}
/*
// Wiring/Arduino code:
// Read data from the serial and turn ON or OFF a light depending on the value
char val; // Data received from the serial port
int ledPin = 4; // Set the pin to digital I/O 4
void setup() {
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
if (Serial.available()) { // If data is available to read,
val = Serial.read(); // read it and store it in val
}
if (val == 'H') { // If H was received
digitalWrite(ledPin, HIGH); // turn the LED on
} else {
digitalWrite(ledPin, LOW); // Otherwise turn it OFF
}
delay(100); // Wait 100 milliseconds for next reading
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment