Created
March 22, 2020 17:59
-
-
Save owencsmith/9ad17414d294096986c9afa6b99bebe1 to your computer and use it in GitHub Desktop.
Arduino sketch for the dispensing system.
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
/* | |
* Sketch to Control Dispensing | |
*/ | |
#include <SPI.h> | |
#include <Servo.h> | |
#include <Wire.h> | |
#define SLAVE_ADDRESS 0x10 | |
#define MAX_ANGLE 180 | |
#define MIN_ANGLE 0 | |
const int dropPin = 9; | |
const int preparePin = 12; | |
Servo dropCupServo; | |
Servo prepareCupServo; | |
volatile bool receiveFlag = false; | |
int receiveBuffer[9]; | |
void setup() { | |
dropCupServo.attach(dropPin); | |
prepareCupServo.attach(preparePin); | |
//Setup for I2C | |
Serial.begin(9600); // start serial for output | |
Wire.begin(SLAVE_ADDRESS); | |
Wire.onReceive(receiveData); | |
Wire.onRequest(sendData); | |
Serial.println("I2C Ready!"); | |
} | |
void loop() { | |
delay(100); | |
if(receiveFlag){ | |
runOnReceivedData(); | |
receiveFlag = false; | |
} | |
} | |
/** | |
* Right now we don't care what the message is it just means go | |
*/ | |
void receiveData(int byteCount){ | |
while(Wire.available()){ | |
Wire.read(); | |
} | |
receiveFlag = true; | |
} | |
void sendData(){ | |
if (receiveBuffer[0] == 99){ | |
//writeData(keepCount()); | |
} | |
else{ | |
Serial.println("No function for this address"); | |
} | |
} | |
/** | |
* Runs the servo one direction and then the other | |
*/ | |
void runOnReceivedData(){ | |
//Drop the cup | |
dropCupServo.write(MAX_ANGLE); | |
delay(250); | |
dropCupServo.write(MIN_ANGLE); | |
delay(250); | |
//Prepare the next cup | |
prepareCupServo.write(MAX_ANGLE); | |
delay(250); | |
prepareCupServo.write(MIN_ANGLE); | |
delay(250); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment