Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save paulgorman/c713b283dc68a566b80c to your computer and use it in GitHub Desktop.
Save paulgorman/c713b283dc68a566b80c to your computer and use it in GitHub Desktop.
/*********************************************************************
Presence's fucking simon clock thing
*********************************************************************/
#include <Arduino.h>
#include <SoftwareSerial.h>
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"
#include "BluefruitConfig.h"
/*=========================================================================
MODE_LED_BEHAVIOUR LED activity, valid options are
"DISABLE" or "MODE" or "BLEUART" or
"HWUART" or "SPI" or "MANUAL"
-----------------------------------------------------------------------*/
#define FACTORYRESET_ENABLE 0
#define MINIMUM_FIRMWARE_VERSION "0.6.6"
#define MODE_LED_BEHAVIOUR "BLUEUART"
/*=========================================================================*/
SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN);
Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN,BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN);
// Presence's Magic Number:
const int stepsPerRevolution = 860; // change this to fit the number of steps per revolution
// yay
String inputTime = "";
int hour;
int minute;
int totalSteps;
int hourSteps;
int minuteSteps;
int stepCount = 0;
// A small helper
void error(const __FlashStringHelper*err) {
Serial.println(err);
while (1);
}
void setup(void) {
pinMode(5, OUTPUT); // Stepper Trigger
pinMode(9, OUTPUT); // Direction
pinMode(13, OUTPUT); // LED
Serial.begin(115200);
Serial.println(F("Simon's Clock"));
Serial.println(F("------------------------------------------------"));
/* Initialise the module */
Serial.print(F("Initialising the Bluefruit LE module: "));
if ( !ble.begin(VERBOSE_MODE) ) {
error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
}
Serial.println( F("OK!") );
if ( FACTORYRESET_ENABLE ) {
/* Perform a factory reset to make sure everything is in a known state */
Serial.println(F("Performing a factory reset: "));
if ( ! ble.factoryReset() ){
error(F("Couldn't factory reset"));
}
}
/* Disable command echo from Bluefruit */
ble.echo(false);
Serial.println("Requesting Bluefruit info:");
/* Print Bluefruit information */
ble.info();
Serial.println(F("Please use Adafruit Bluefruit LE app to connect in UART mode"));
Serial.println(F("Then send 4 digits to set the clock"));
Serial.println();
ble.verbose(false); // debug info is a little annoying after this point!
/* Wait for connection */
digitalWrite(13,HIGH);
while (! ble.isConnected()) {
delay(500);
}
Serial.println(F("******************************"));
// LED Activity command is only supported from 0.6.6
if ( ble.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION) ) {
// Change Mode LED Activity
Serial.println(F("Change LED activity to " MODE_LED_BEHAVIOUR));
ble.sendCommandCheckOK("AT+HWModeLED=" MODE_LED_BEHAVIOUR);
}
// Set module to DATA mode
Serial.println( F("Switching to DATA mode!") );
ble.setMode(BLUEFRUIT_MODE_DATA);
digitalWrite(13,LOW);
Serial.println(F("******************************"));
}
void loop(void) {
// Check for user input
char n, inputs[BUFSIZE+1];
if (Serial.available())
{
n = Serial.readBytes(inputs, BUFSIZE);
inputs[n] = 0;
// Send characters to Bluefruit
Serial.print("Sending: ");
Serial.println(inputs);
// Send input data to host via Bluefruit
ble.print(inputs);
}
// Echo received data
while ( ble.available() ) {
char c = ble.read();
Serial.print((char)c);
if ( ( c >= '0') && ( c <= '9') ) {
inputTime.concat(c);
}
}
if (inputTime.length() >= 4) {
Serial.print("inputTime is: "); Serial.println(inputTime);
String hourS = inputTime.substring(0,2);
hour = hourS.toInt();
String minuteS = inputTime.substring(2,4);
minute = minuteS.toInt();
hourSteps = hour * stepsPerRevolution;
minuteSteps = minute * (stepsPerRevolution / 60); // magic number by minutes
Serial.print("Processing "); Serial.print(hour); Serial.print(":"); Serial.println(minute);
totalSteps = hourSteps + minuteSteps;
Serial.print("Clicking "); Serial.print(totalSteps); Serial.println(" times.");
while (stepCount <= totalSteps) {
digitalWrite(13,HIGH);
digitalWrite(5,HIGH);
delay(3);
digitalWrite(5,LOW);
delay(3);
stepCount++;
}
digitalWrite(13,LOW);
inputTime = String(""); // reset for next round
Serial.println("Done Moving");
stepCount = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment