Skip to content

Instantly share code, notes, and snippets.

@Electronza
Created May 6, 2020 06:24
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 Electronza/7b393021ed6a013df90f1f0677fb3ef7 to your computer and use it in GitHub Desktop.
Save Electronza/7b393021ed6a013df90f1f0677fb3ef7 to your computer and use it in GitHub Desktop.
SpeakUp Arduino code
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN 33
// Used to reset the SpeakUp click
#define RstPIN 34
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 16
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
String mystring="";
int rcvd;
char incomingByte; // keeps the received command from SpeakUp click
char incomingByte1; // keeps the received command from SpeakUp click
char dimvalue = 150;
int inbuffer=0;
char red;
char green;
char blue;
void update_pixels(void) {
for(int i=0;i<NUMPIXELS;i++){
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(red,green,blue)); // Moderately bright green color.
pixels.show(); // This sends the updated pixel color to the hardware.
}
}
void setup() {
// Reset the SpeakUp click
pinMode(RstPIN, OUTPUT);
digitalWrite(RstPIN, LOW);
delay(1);
digitalWrite(RstPIN, HIGH);
pixels.begin(); // This initializes the NeoPixel library.
// all pixels are off
red = 0;
blue = 0;
green = 0;
update_pixels();
// just a test, turn first neopixel on for a second
pixels.setPixelColor(0, pixels.Color(0,0,150));
pixels.show();
delay(1000);
pixels.setPixelColor(0, pixels.Color(0,0,0));
pixels.show();
// Start serial communication
Serial.begin(9600);
Serial2.begin(9600);
// wait for ready signal
pixels.setPixelColor(0, pixels.Color(0,20,0));
pixels.show();
Serial.println("Waiting for SpeakUp click to come online");
while (Serial2.available() < 5){
// Wait for five characters
}
Serial.print ("Received data = ");
for(int i=0 ; i<5 ; i++){
incomingByte = Serial2.read();
Serial.write(incomingByte);
// add incoming data to string;
mystring += incomingByte;
}
Serial.println("");
if (mystring == "READY"){
Serial.println ("Ready to proceed with voice recognition");
pixels.setPixelColor(0, pixels.Color(0,0,0));
pixels.show();
}
else
{
pixels.setPixelColor(0, pixels.Color(50,0,0));
pixels.show();
Serial.println ("Garbage received. Halting!");
while(1);
}
}
void loop() {
// send data only when you receive data:
if (Serial2.available() > 1) {
// read the incoming byte:
incomingByte = Serial2.read();
incomingByte1 = Serial2.read();
Serial.print ("Received data = ");
Serial.print(int(incomingByte) ,HEX);
Serial.println(int(incomingByte1) ,HEX);
if (incomingByte == 0){ // white
red = dimvalue;
green = dimvalue;
blue = dimvalue;
update_pixels();
Serial.println ("Command: white");
}
if (incomingByte == 1){ // red
red = dimvalue;
green = 0;
blue = 0;
update_pixels();
Serial.println ("Command: red");
}
if (incomingByte == 2){ // blue
red = 0;
green = 0;
blue = dimvalue;
update_pixels();
Serial.println ("Command: blue");
}
if (incomingByte == 3){ // green
red = 0;
green = dimvalue;
blue = 0;
update_pixels();
Serial.println ("Command: green");
}
if (incomingByte == 4){ // brighter
dimvalue = dimvalue + 20;
if (dimvalue > 150) dimvalue = 150; // not too dim
if (red > 0) red = dimvalue;
if (green > 0) green = dimvalue;
if (blue > 0) blue = dimvalue;
update_pixels();
Serial.println ("Command: brighter");
}
if (incomingByte == 5){ // dimmer
dimvalue = dimvalue - 20;
if (dimvalue < 10) dimvalue = 10; // not too dim
if (red > 0) red = dimvalue;
if (green > 0) green = dimvalue;
if (blue > 0) blue = dimvalue;
update_pixels();
Serial.println ("Command: dimmer");
}
if (incomingByte == 6){ // turn off
red = 0;
green = 0;
blue = 0;
update_pixels();
Serial.println ("Command: turn off");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment