0% found this document useful (0 votes)
62 views26 pages

Include Include Include Include

This document defines code for an ESP8266 microcontroller that controls NeoPixel LED strips, servos, and a motor driver. It connects to WiFi, sets up an access point, and exposes sensor values and functions to control devices through a REST API. The functions allow commands to be run, servos to be calibrated and attached, and the motor driver to be configured. Pin values are read periodically and published to the REST API.

Uploaded by

gabrielapb19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views26 pages

Include Include Include Include

This document defines code for an ESP8266 microcontroller that controls NeoPixel LED strips, servos, and a motor driver. It connects to WiFi, sets up an access point, and exposes sensor values and functions to control devices through a REST API. The functions allow commands to be run, servos to be calibrated and attached, and the motor driver to be configured. Pin values are read periodically and published to the REST API.

Uploaded by

gabrielapb19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

// Import required libraries

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <aREST.h>
#include <Servo.h>

// Variables to be exposed to the API


int build = 51;
// GPIO mapping
int gpio[] = {16,5,4,0,2,14,12,13,15};
// NeoPixel
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(24, gpio[5], NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(24, gpio[6], NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip3 = Adafruit_NeoPixel(24, gpio[7], NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip4 = Adafruit_NeoPixel(24, gpio[8], NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip = strip1;
// aREST Pro key (that you can get at dashboard.arest.io)
char * key = "yourkey";
char * deviceName = "YourDeviceName"; // change the number for every robot
// Pin Values
int PinA0;
int PinD0;
int PinD1;
int PinD2;
int PinD3;
int PinD4;
int PinD5;
int PinD6;
int PinD7;
int PinD8;
// Clients
WiFiClient espClient;
PubSubClient client(espClient);

// Create aREST instance


aREST restAP = aREST();
aREST rest = aREST(client);
// Start the server
#define LISTEN_PORT 80
WiFiServer server(LISTEN_PORT);
// WiFi parameters
const char* ssid = "your wifi name";
const char* password = "Your Wifi Password";
const char* ssidAP = deviceName;
const char* passwordAP = "YourDevicePassword";
// Functions
void callback(char* topic, byte* payload, unsigned int length);
// Variables to be exposed to the API
String analogValues = "";
String digitalValues = "";
// Declare functions to be exposed to the API
int calibrate(String command);
int commands(String command);
int logo(String command);
int runCommands(String command);
int attachServos(String command);
int setMotorDriver(String command);
int readPins(String command);
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
// Other variables
int bridged = 0;
int enableAP = 1;
int enableCloud = 0;
int maxPinsIndex = 8;
int maxCommandQueueIndex = 19;
int maxServosIndex = 3;
int logoQueueIndex = 0;
int maxLogoQueueIndex = 20;
int motorPinsConfigured = 0;
int maxPWM = 1023;
int secondsForWiFiConnection = 9;
int wifiConnected = 0;
unsigned long previousMillisWiFi = 0;
const long intervalWiFi = 3000; // check to see if WiFi connected ever x
milliseconds while looping
unsigned long previousMillisPro = 0;
const long intervalPro = 500; // x milliseconds between Pro connections
unsigned long previousMillisPinRead = 0;
const long intervalPinRead = 500; // x milliseconds between updating pin state
int motorDriver = 1; // default motor driver.
int pinA1A = 1; // default pin for left motor Forward
int pinA1B = 2; // left motor backwards
int pinB1A = 3; // right motor Forward
int pinB1B = 4; // right motor Backwards
//int pinLPWM = 4; // default pin for left motor PWM
//int pinRPWM = 5; // default pin for right motor PWM
int pinPen = 5; // default pin for Logo Pen
int leftPWMF = 1023; // default PWM value for the left motor
int rightPWMF = 1023; // default PWM value for the right motor
int leftPWMB = 1023; // default PWM value for the left motor
int rightPWMB = 1023; // default PWM value for the right motor
int speedFD = 100; // default forward speed in mm/second
int speedBK = 100; // default backward speed in mm/second
int speedLT = 180; // default left rotation speed in degrees/second
int speedRT = 180; // default right rotation speed in degrees/second
int speedARCFL = 90; // default forward left arc speed in degrees/second
int speedARCFR = 90; // default forward right arc speed in degrees/second
int speedARCBL = 90; // default back left arc speed in degrees/second
int speedARCBR = 90; // default back right arc speed in degrees/second
int currentLogoCommand[2]; // [command,value,endMillis]
unsigned long currentLogoCommandExpiry;
int commandQueue[9][19][3]; // [pin 0-19][command# 0-max][0:format 1:value
2:duration]
int commandQueueIndex[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // the current
index for each pin in commandQueue
unsigned long commandTimeout[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // the
time at which the command for a pin times out
int logoQueue[20][2]; // [command#] [command,value]
int servoArray[4]; // the pin numbers for the servos
unsigned long lastRandom = 0;
int hashMapLength = 50; // size of our hashMap
int hashMapIndex = 0; // next index to use
String hashMapKeys[50];
String hashMapValues[50];
void setup(void)
int delayMilli = 500;
int countMilli = 0;
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(delayMilli);
Serial.print(".");
countMilli += delayMilli;
if(countMilli/1000 > secondsForWiFiConnection){
break;
}
}

if(WiFi.status() == WL_CONNECTED){
wifiConnected = 1;
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP ");
Serial.println(WiFi.localIP());
delay(500);
} else {
// Make sure WiFi is disconnected
WiFi.disconnect();
delay(50);
wifiConnected = 0;

Serial.println("");
Serial.println("*** WiFi NOT connected ***");
}
char* out_topic = rest.get_topic();
if(enableCloud == 1){
Serial.println("Enabled");
}else{
WiFi.softAPdisconnect(true);
delay(50);

if(enableAP == 0){
Serial.println("AP Mode disabled");
}
// Init variables and expose them to REST API
restAP.variable("build",&build);
restAP.variable("analogValues",&analogValues);
restAP.variable("digitalValues",&digitalValues);
// Function to be exposed
restAP.function("calibrate",calibrate);
restAP.function("commands",commands);
restAP.function("logo",logo);
restAP.function("runCommands",runCommands);
restAP.function("attachServos",attachServos);
restAP.function("setMotorDriver",setMotorDriver);
restAP.function("readPins",readPins);
restAP.set_id("local");
restAP.set_name(deviceName);
if(enableAP == 1){
WiFi.softAP(ssidAP, passwordAP);
Serial.println("");
Serial.println("WiFi AP: Access Point created");
Serial.print("SSID AP: ");
Serial.println(ssidAP);
Serial.print("Password: ");
Serial.println(passwordAP);
Serial.println("");

// Print the IP address


IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);

server.begin();

Serial.println("");
Serial.println("-----------------------------");
Serial.println("Web Server");
Serial.println("-----------------------------");
Serial.println("Started");
delay(500);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillisWiFi >= intervalWiFi) {
// save the last time you checked
previousMillisWiFi = currentMillis;

if(wifiConnected == 1){
if(WiFi.status() != WL_CONNECTED){
WiFi.disconnect();
delay(50);
wifiConnected = 0;

}
}
}

runLogo();
runCommands("");
if(enableCloud == 1){

if (wifiConnected && currentMillis - previousMillisPro >= intervalPro) {


// save the last time you checked
previousMillisPro = currentMillis;
// Connect to the cloud
rest.handle(client);
}
}
WiFiClient clientAP = server.available();
if (!clientAP) {
return;
}

readPins(""); // if the clientAP exists, it means we probably need latest pin


values

if(!clientAP.available()){
Serial.print("NOT clientAP.available() ,");

runLogo();

runCommands("");

delay(1);

}else{

restAP.handle(clientAP);
}

}
// Handles message arrived on subscribed topic(s)
void callback(char* topic, byte* payload, unsigned int length) {
rest.handle_callback(client, topic, payload, length);

}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {
0, -1 };
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
String rVal = "";
if(found>index){
rVal = data.substring(strIndex[0], strIndex[1]);
rVal.replace(" ","");
}

return rVal;
}
// Custom function accessible by the API
int readPins(String command) {
Serial.print("rP ");
bool readAll = false;

if(command == "")
readAll = true;

int wait = 10;


command = ","+command+",";

String str_Command;
int item_counter = 0;
while(1){
str_Command = getValue(command, ',', item_counter++);
if(!readAll && str_Command == "")
break;
if(readAll || str_Command == "A0"){
PinA0 = analogRead(A0);
delay(wait);
}
if(readAll || str_Command == "D0"){
PinD0 = digitalRead(gpio[0]);
delay(wait);
}
if(readAll || str_Command == "D1"){
PinD1 = digitalRead(gpio[1]);
delay(wait);
}
if(readAll || str_Command == "D2"){
PinD2 = digitalRead(gpio[2]);
delay(wait);
}
if(readAll || str_Command == "D3"){
PinD3 = digitalRead(gpio[3]);
delay(wait);
}
if(readAll || str_Command == "D4"){
PinD4 = digitalRead(gpio[4]);
delay(wait);
}
if(readAll || str_Command == "D5"){
PinD5 = digitalRead(gpio[5]);
delay(wait);
}
if(readAll || str_Command == "D6"){
PinD6 = digitalRead(gpio[6]);
delay(wait);
}
if(readAll || str_Command == "D7"){
PinD7 = digitalRead(gpio[7]);
delay(wait);
}
if(readAll || str_Command == "D8"){
PinD8 = digitalRead(gpio[8]);
delay(wait);
}

break;
}

analogValues = String(PinA0);
digitalValues = String(PinD0) + "," + String(PinD1) + "," + String(PinD2) + "," +
String(PinD3) + "," + String(PinD4) + "," + String(PinD5) + "," + String(PinD6) +
"," + String(PinD7) + "," + String(PinD8);

return 1;
}
int commands(String command) {
String str_Command;
int item_counter = 0;

while(1){
str_Command = getValue(command, ':', item_counter++);
if(str_Command == "")
break;
addCommandToQueue(str_Command);

}
return item_counter;
}
void addCommandToQueue(String command){
String data;
int formatInt;
int pinInt;
int valueInt;
String value = "";
int durationInt;

int item_counter = 0;

while(1){
data = getValue(command, ',', item_counter++);
if(data == "")
break;
formatInt = data.toInt();

data = getValue(command, ',', item_counter++);


if(data == "")
break;
pinInt = data.toInt();
if(pinInt < 0 || pinInt > maxPinsIndex)
break;
data = getValue(command, ',', item_counter++);
if(data == "")
break;
if(hasStringValue(formatInt)){
value = data; // Also save the string value
valueInt = getRandom(1000,2000);
setHashValue(String(valueInt),value);
}else{
valueInt = data.toInt();
}

data = getValue(command, ',', item_counter++);


// default duration is 0 (if ""), i.e. indefinite duration
durationInt = data.toInt();

int commandIndex = commandQueueIndex[pinInt];


if(commandIndex > maxCommandQueueIndex)
break;

commandQueue[pinInt][commandIndex][0] = formatInt;
commandQueue[pinInt][commandIndex][1] = valueInt;
commandQueue[pinInt][commandIndex][2] = durationInt;

// increment the index, for the next command to use


commandQueueIndex[pinInt]++;

break;
}

}
int runLogo(){
unsigned long currentMillis = millis();
if(currentLogoCommand[0] >= 0 && currentLogoCommandExpiry > currentMillis)
return 1;

else {
if(logoQueueIndex > 0){
// save the new command details
int new_command = logoQueue[0][0];
int new_value = logoQueue[0][1];
for(int pos=0;pos<(logoQueueIndex-1);pos++){
logoQueue[pos][0] = logoQueue[(pos+1)][0];
logoQueue[pos][1] = logoQueue[(pos+1)][1];
}
// make sure the last one is removed (set to -1)
logoQueue[(logoQueueIndex-1)][0] = -1;
logoQueue[(logoQueueIndex-1)][1] = -1;
logoQueueIndex--;
executeLogo(new_command,new_value);

} else{
if(currentLogoCommandExpiry == 0)
return 1;
else if(currentLogoCommand[0] == 0)
return 1; // current command is STOP, so don't re-execute
else{
// else stop the command
executeLogo(0);
}

}
}
int executeLogo(int command){
return executeLogo(command,0);
}
int executeLogo(int command, int value){
int duration = getLogoDuration(command,value);
unsigned long currentMillis = millis();
currentLogoCommand[0] = command;
currentLogoCommand[1] = value;
currentLogoCommandExpiry = (currentMillis + (long)duration);
// Special case for indefinite commands
if(value == 0)
currentLogoCommandExpiry = 0;
if(motorPinsConfigured == 0){
initialiseMotorPins();
pinMode(gpio[pinPen], OUTPUT);
digitalWrite(gpio[pinPen],1); // Put the pen down by default.

}
if(command == 0){
motorLeftST();
motorRightST();
}
if(command == 1){
motorLeftFD();
motorRightFD();
}
if(command == 2){
motorLeftBK();
motorRightBK();
}
if(command == 3){
motorLeftBK();
motorRightFD();
}
if(command == 4){
motorLeftFD();
motorRightBK();
}
if(command == 5){
motorLeftST();
motorRightFD();
}
if(command == 6){
motorRightST();
motorLeftFD();
}
if(command == 7){
motorLeftST();
motorRightBK();
}
if(command == 8){
motorRightST();
motorLeftBK();
}
if(command == 9){
// Pen Up. Default behaviour turns the pinPen on and off
digitalWrite(gpio[pinPen],0);
}
// 10. PD (or PENDOWN) - Put the Pen down
if(command == 10){
// Pen Down. Default behaviour turns the pinPen on and off. Other Option to move
servo
digitalWrite(gpio[pinPen],1);
}
return 1;
}
void motorLeftST(){
if(motorDriver == 1){
analogWrite(gpio[pinA1A],0);
analogWrite(gpio[pinA1B],0);
digitalWrite(gpio[pinA1A],0);
digitalWrite(gpio[pinA1B],0);

} else if(motorDriver == 2){

} else {
analogWrite(gpio[pinA1A],0);
digitalWrite(gpio[pinB1A],0);
}
}

void motorRightST(){
if(motorDriver == 1){
analogWrite(gpio[pinB1A],0);
analogWrite(gpio[pinB1B],0);
digitalWrite(gpio[pinB1A],0);
digitalWrite(gpio[pinB1B],0);
} else if(motorDriver == 2){

} else {
analogWrite(gpio[pinA1B],0);
digitalWrite(gpio[pinB1B],0);
}
}

void motorLeftFD(){
if(motorDriver == 1){
digitalWrite(gpio[pinA1A],1);
analogWrite(gpio[pinA1B],(maxPWM - leftPWMF));
} else if(motorDriver == 2){

} else {
analogWrite(gpio[pinA1A],leftPWMF);
digitalWrite(gpio[pinB1A],1);
}
}
void motorRightFD(){
if(motorDriver == 1){
digitalWrite(gpio[pinB1A],1);
analogWrite(gpio[pinB1B],(maxPWM - rightPWMF));
} else if(motorDriver == 2){

} else {
analogWrite(gpio[pinA1B],rightPWMF);
digitalWrite(gpio[pinB1B],1);
}
}
void motorLeftBK(){
if(motorDriver == 1){
digitalWrite(gpio[pinA1A],0);
analogWrite(gpio[pinA1B],leftPWMB);
} else if(motorDriver == 2){

} else {
analogWrite(gpio[pinA1A],leftPWMF);
digitalWrite(gpio[pinB1A],0);
}
}

void motorRightBK(){
if(motorDriver == 1){
digitalWrite(gpio[pinB1A],0);
analogWrite(gpio[pinB1B],rightPWMB);
} else if(motorDriver == 2){

} else {
analogWrite(gpio[pinA1B],rightPWMF);
digitalWrite(gpio[pinB1B],0);
}
}

void initialiseMotorPins(){
pinMode(gpio[pinA1A], OUTPUT);
pinMode(gpio[pinA1B], OUTPUT);
pinMode(gpio[pinB1A], OUTPUT);
pinMode(gpio[pinB1B], OUTPUT);
motorPinsConfigured = 1;
}
int getLogoDuration(int command, int value){
if(value == 0)
return 0;

if(command == 0)
return value;
if(command == 1)
return (int)(1000.0*(float)value/(float)speedFD);
if(command == 2)
return (int)(1000.0*(float)value/(float)speedBK);
if(command == 3)
return (int)(1000.0*(float)value/(float)speedLT);
if(command == 4)
return (int)(1000.0*(float)value/(float)speedRT);
if(command == 5)
return (int)(1000.0*(float)value/(float)speedARCFL);
if(command == 6)
return (int)(1000.0*(float)value/(float)speedARCFR);
if(command == 7)
return (int)(1000.0*(float)value/(float)speedARCBL);
if(command == 8)
return (int)(1000.0*(float)value/(float)speedARCBR);

return 0;

}
int calibrate(String command){
String str_Pair;
String str_Constant;
String str_Value;

int item_counter = 0;
while(1){
// Get the next Pair from the command string
str_Pair = getValue(command, ';', item_counter++);
if(str_Pair == "")
break;

// Break it into Command,Value


while(1){
str_Constant = getValue(str_Pair, ',', 0);
if(str_Constant == "")
break;
str_Value = getValue(str_Pair, ',', 1);
if(str_Value == "")
break;
int iValue = str_Value.toInt();

updateConstant(str_Constant,iValue);
break;

}
}
return item_counter;
}
int updateConstant(String c, int value){
// Update the 'constant' c setting it to value

if(c == "leftPWMF")
leftPWMF = value;
if(c == "rightPWMF")
rightPWMF = value;
if(c == "leftPWMB")
leftPWMB = value;
if(c == "rightPWMB")
rightPWMB = value;
if(c == "speedFD")
speedFD = value;
if(c == "speedBK")
speedBK = value;
if(c == "speedLT")
speedLT = value;
if(c == "speedRT")
speedRT = value;
if(c == "speedARCFL")
speedARCFL = value;
if(c == "speedARCFR")
speedARCFR = value;
if(c == "speedARCBL")
speedARCBL = value;
if(c == "speedARCBL")
speedARCBL = value;
if(c == "speedARCBR")
speedARCBR = value;
return 1;
}
int runCommands(String command){
for(int pinInt=0;pinInt<maxPinsIndex;pinInt++){
int nextCommandIndex = commandQueueIndex[pinInt];
if(nextCommandIndex>0){

// if the timeout has expired, run the next command (index 0)


unsigned long currentMillis = millis();
if(commandTimeout[pinInt] < currentMillis){
int format = commandQueue[pinInt][0][0];
int value = commandQueue[pinInt][0][1];
int duration = commandQueue[pinInt][0][2];

// set the timeout. If duration is 0, then no timeout


if(duration == 0)
commandTimeout[pinInt] = 0;
else
commandTimeout[pinInt] = (currentMillis + duration);

// move each command down one index (Command at 0 will be overwritten)


for(int pos=0;pos<(nextCommandIndex-1);pos++){
commandQueue[pinInt][pos][0] = commandQueue[pinInt][(pos+1)][0];
commandQueue[pinInt][pos][1] = commandQueue[pinInt][(pos+1)][1];
commandQueue[pinInt][pos][2] = commandQueue[pinInt][(pos+1)][2];
}
// make sure the last one is not duplicated
commandQueue[pinInt][(nextCommandIndex-1)][0] = -1;
commandQueue[pinInt][(nextCommandIndex-1)][1] = -1;
commandQueue[pinInt][(nextCommandIndex-1)][2] = -1;
// decrement the commandQueueIndex - because we have removed one of the
commands
commandQueueIndex[pinInt]--;

if(format == 0){
// Analog

pinMode(gpio[pinInt], OUTPUT);
// special case if the pin has a servo attached
if(servoArray[0] == pinInt && servo1.attached())
servo1.write(value);
else if(servoArray[1] == pinInt && servo2.attached())
servo2.write(value);
else if(servoArray[2] == pinInt && servo3.attached())
servo3.write(value);
else if(servoArray[3] == pinInt && servo4.attached())
servo4.write(value);
else
analogWrite(gpio[pinInt],value);

} else if(format == 1){


// Digital

pinMode(gpio[pinInt], OUTPUT);
digitalWrite(gpio[pinInt],value);

} else if(format >= 101 && format <= 199){


// NeoPixel

neoPixelRoutine(format,pinInt,value);
}
}

}
return 1;
}
int attachServos(String command){
String str_Command;
int item_counter = 0;
int item_counter2 = 0;
String data;
int servoNumber = 0;
int pinInt;

while(1){
str_Command = getValue(command, ':', item_counter++);
if(str_Command == "")
break;

while(1){
data = getValue(command, ',', item_counter2++);
if(data == "")
break;
servoNumber = data.toInt();

data = getValue(command, ',', item_counter2++);


if(data == "")
break;
pinInt = data.toInt();
if(pinInt < 0 || pinInt > maxPinsIndex)
break;

if(servoNumber == 1){
if(servo1.attached())servo1.detach();
servo1.attach(gpio[pinInt]);
servoArray[0] = pinInt;
}
if(servoNumber == 2){
if(servo2.attached())servo2.detach();
servo2.attach(gpio[pinInt]);
servoArray[1] = pinInt;
}
if(servoNumber == 3){
if(servo3.attached())servo3.detach();
servo3.attach(gpio[pinInt]);
servoArray[2] = pinInt;
}
if(servoNumber == 4){
if(servo4.attached())servo4.detach();
servo4.attach(gpio[pinInt]);
servoArray[3] = pinInt;
}

}
}

return item_counter;
}
int attachNeoPixel(String command){
return 1;
}
int setMotorDriver(String command){
int type = command.toInt();
if(type >= 0 && type <= 2){
motorDriver = type;

return motorDriver;
}
return -1;
}
// Custom function accessible by the API
int logo(String command) {

command.replace(" /"," ");


String str_CommandValue;
String str_Command;
String str_Value;

int item_counter = 0;
while(1){
// Get the next command,value from the command string
str_CommandValue = getValue(command, ';', item_counter++);
if(str_CommandValue == "")
break;

// Break it into Command,Value


while(1){
str_Command = getValue(str_CommandValue, ',', 0);
if(str_Command == "")
break;
str_Value = getValue(str_CommandValue, ',', 1);
int iValue = 0; // default value is 0
if(str_Value > "")
iValue = str_Value.toInt();

addLogoToQueue(str_Command,iValue);
break;

}
return item_counter;
}
int addLogoToQueue(String command, int value){
if(logoQueueIndex > maxLogoQueueIndex)
return 0;
command.toUpperCase();
int int_command;
if(command == "ST" || command == "STOP")
int_command = 0;
else if(command == "FD" || command == "FORWARD")
int_command = 1;
else if(command == "BK" || command == "BACK")
int_command = 2;
else if(command == "LT" || command == "LEFT")
int_command = 3;
else if(command == "RT" || command == "RIGHT")
int_command = 4;
else if(command == "ARCFL")
int_command = 5;
else if(command == "ARCFR")
int_command = 6;
else if(command == "ARCBL")
int_command = 7;
else if(command == "ARCBR")
int_command = 8;
else if(command == "PU" || command == "PENUP")
int_command = 9;
else if(command == "PD" || command == "PENDOWN")
int_command = 10;
else
return 0; // command not supported

logoQueue[logoQueueIndex][0] = int_command;
logoQueue[logoQueueIndex][1] = value;

logoQueueIndex++;

return 1;
}
void neoPixelRoutine(int routine, int s, int value){
String params = String(value);
String strRoutine = params.substring(0,2);
String strWait = params.substring(2,4);
String strColor = params.substring(6,20);
if(routine == 101)
np_clear(s);

if(routine == 102)
np_setColor(s,getHashValue(value).toInt());
}
int np_clear(int s) {
return np_setColor(s,0);
}
int np_setColor(int s, uint32_t c) {
if(s==1){strip = strip1;} else if(s==2){strip = strip2;} else if(s==3){strip =
strip3;} else if(s==4){strip = strip4;} else return 0;

for(uint16_t i=0; i<strip.numPixels(); i++) {


strip.setPixelColor(i, c);
}
strip.show();
return 1;
}
int np_flash(int s, uint32_t c1, uint32_t c2, uint8_t wait, int counter) {
if(s==1){strip = strip1;} else if(s==2){strip = strip2;} else if(s==3){strip =
strip3;} else if(s==4){strip = strip4;} else return 0;

for(int a=0; a<counter; a++) {


for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c1);
}
strip.show();
delay(wait);

for(uint16_t i=0; i<strip.numPixels(); i++) {


strip.setPixelColor(i, c2);
}
strip.show();
delay(wait);
}
return 1;
}
int np_colorWipe(int s, uint32_t c, uint8_t wait) {

if(s==1){strip = strip1;} else if(s==2){strip = strip2;} else if(s==3){strip =


strip3;} else if(s==4){strip = strip4;} else return 0;

for(uint16_t i=0; i<strip.numPixels(); i++) {


strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
return 1;
}

void np_rainbow(Adafruit_NeoPixel st, uint8_t wait) {


uint16_t i, j;

for(j=0; j<256; j++) {


for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, np_Wheel(st,(i+j) & 255));
}
strip.show();
delay(wait);
}
}
void np_rainbowCycle(Adafruit_NeoPixel st, uint8_t wait) {
uint16_t i, j;

for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel


for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, np_Wheel(st,((i * 256 / strip.numPixels()) + j) &
255));
}
strip.show();
delay(wait);
}
}
void np_theaterChase(Adafruit_NeoPixel st, uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();

delay(wait);

for (uint16_t i=0; i < strip.numPixels(); i=i+3) {


strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
void np_theaterChaseRainbow(Adafruit_NeoPixel st, uint8_t wait) {
for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
for (int q=0; q < 3; q++) {
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, np_Wheel(st, (i+j) % 255)); //turn every third
pixel on
}
strip.show();

delay(wait);

for (uint16_t i=0; i < strip.numPixels(); i=i+3) {


strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
uint32_t np_Wheel(Adafruit_NeoPixel st, byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return st.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return st.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return st.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
String getHashValue(int key){
return getHashValue(String(key));
}

String getHashValue(String key){

for (int i=0; i < hashMapIndex; i++) {


if(hashMapKeys[i] == key){
return hashMapValues[i];
}
}
return "";
}

void setHashValue(String key, String value){


int done = 0;
for (int i=0; i < hashMapIndex; i++) {
if(hashMapKeys[i] == key){
hashMapValues[i] = value;
done = 1;
}
}

if(done == 0){
hashMapKeys[hashMapIndex] = key;
hashMapValues[hashMapIndex] = value;
hashMapIndex ++;
}

int getRandom(int from,int to){


randomSeed(lastRandom);
lastRandom = (int) random(from,to);
return lastRandom;
}

int hasStringValue(int formatInt){


if(formatInt > 100)
return 1;

return 0;
}
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

You might also like