Skip to content

Instantly share code, notes, and snippets.

@emptyset
Last active August 29, 2015 14:16
Show Gist options
  • Save emptyset/07c379d965c7630cbf85 to your computer and use it in GitHub Desktop.
Save emptyset/07c379d965c7630cbf85 to your computer and use it in GitHub Desktop.
Code for the JAM!
int motorPin = 3;
int lightsPin = 5;
int limitPin = 7;
int buttonPin = 8;
int buttonLightPin = 4;
boolean active = false;
boolean motorRunning = false;
int dispenseSeconds = 300;
int motorRunMaxSeconds = 20;
unsigned long motorTime = 0;
void setup() {
pinMode(motorPin, OUTPUT);
pinMode(lightsPin, OUTPUT);
pinMode(buttonLightPin, OUTPUT);
pinMode(limitPin, INPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
while (!Serial);
Serial.println("Start machine");
}
void loop() {
if (active) {
if (digitalRead(buttonPin) == HIGH) {
Serial.println("button pushed");
Serial.println("run the motor");
digitalWrite(buttonLightPin, LOW);
runMotor();
// goNuts();
Serial.println("inactivate machine");
active = false;
Serial.println("turn off lights");
delay(5000);
digitalWrite(lightsPin, LOW);
}
} else {
Serial.println("wait to dispense");
delay(dispenseSeconds * 1000);
Serial.println("activate machine");
active = true;
Serial.println("turn on lights");
digitalWrite(lightsPin, HIGH);
digitalWrite(buttonLightPin, HIGH);
}
}
void runMotor() {
motorRunning = true;
motorTime = millis() + motorRunMaxSeconds * 1000;
digitalWrite(motorPin, HIGH);
while (motorRunning) {
delay(100);
if (digitalRead(limitPin) == LOW ||
millis() >= motorTime)
{
Serial.println("limit switch hit");
digitalWrite(motorPin, LOW);
motorRunning = false;
}
}
}
void goNuts() {
for (int count = 0; count < 10; count++) {
digitalWrite(lightsPin, HIGH);
digitalWrite(buttonLightPin, HIGH);
delay(250);
digitalWrite(lightsPin, LOW);
digitalWrite(buttonLightPin, LOW);
delay(250);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment