Last active
September 13, 2015 12:46
-
-
Save mithi/e5f4b9b2901223955ec0 to your computer and use it in GitHub Desktop.
merging crazy patterns with IR
This file contains hidden or 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
#include <Sparki.h> | |
class Patterns{ | |
public: | |
Patterns(){ | |
} | |
void turnSignal(){ | |
int colors[3][3] = {RGB_RED, RGB_BLUE, RGB_GREEN}; | |
sparki.beep(); | |
for (int x = 0; x < 3; x++){ | |
sparki.RGB(colors[x][0], colors[x][1], colors[x][2]); | |
delay(250); | |
} | |
sparki.RGB(RGB_OFF); | |
} | |
void stopSignal(){ | |
for (int x = 1; x < 10; x++){ | |
sparki.beep(); | |
sparki.RGB(50, 0, 100); | |
delay(200); | |
} | |
sparki.RGB(RGB_OFF); | |
} | |
void stopMotors(){ | |
sparki.motorStop(MOTOR_LEFT); | |
sparki.motorStop(MOTOR_RIGHT); | |
stopSignal(); | |
} | |
void forwardWheels(int leftSpeed, int rightSpeed, int waitmSec) { | |
turnSignal(); | |
sparki.motorRotate(MOTOR_LEFT, DIR_CCW, leftSpeed); | |
sparki.motorRotate(MOTOR_RIGHT, DIR_CW, rightSpeed); | |
delay(waitmSec); | |
} | |
void reverseWheels(int leftSpeed, int rightSpeed, int waitmSec) { | |
turnSignal(); | |
sparki.motorRotate(MOTOR_LEFT, DIR_CW, leftSpeed); | |
sparki.motorRotate(MOTOR_RIGHT, DIR_CCW, rightSpeed); | |
delay(waitmSec); | |
} | |
void crazySun(int speed1, int speed2, int waitmSec, unsigned long timeToDo){ | |
unsigned long endTime = millis() + timeToDo; | |
while(millis() < endTime){ | |
forwardWheels(speed1, speed2, waitmSec); | |
reverseWheels(speed2, speed1, waitmSec); | |
} | |
stopMotors(); | |
} | |
void squigglyLines(int speed1, int speed2, int waitmSec, int num){ | |
for (int x=0; x<num; x++){ | |
forwardWheels(speed1, speed2, waitmSec); | |
forwardWheels(speed2, speed1, waitmSec); | |
} | |
stopMotors(); | |
} | |
void circle(int speed1, int speed2, int waitmSec, unsigned long timeToDo){ | |
unsigned long endTime = millis() + timeToDo; | |
while(millis() < endTime) { | |
forwardWheels(speed1, speed2, waitmSec); | |
} | |
stopMotors(); | |
} | |
void hourglassHelper(int x1, int x2, int w){ | |
sparki.moveForward(x1); | |
sparki.moveLeft(90); | |
sparki.moveForward(w); | |
sparki.moveLeft(90); | |
sparki.moveForward(x2); | |
sparki.moveRight(90); | |
sparki.moveForward(w); | |
sparki.moveRight(90); | |
} | |
void hourglass(int l, int w){ | |
for(int x = l; x > 0; x-=2){ | |
hourglassHelper(x, x-1, w); | |
} | |
turnSignal(); | |
for(int x = 1; x < l; x+=2){ | |
hourglassHelper(x, x+1, w); | |
} | |
stopMotors(); | |
} | |
void mazeHelperRight(int x){ | |
sparki.moveForward(x); | |
sparki.moveRight(90); | |
} | |
void mazeHelperLeft(int x){ | |
sparki.moveForward(x); | |
sparki.moveLeft(90); | |
} | |
void maze(int l, int w){ | |
for (int x = 1; x <= 2*l; x+=w){ | |
mazeHelperRight(x); | |
} | |
sparki.moveRight(270); | |
for (int x = 2*l; x > 0; x-=w){ | |
mazeHelperLeft(x); | |
} | |
stopMotors(); | |
} | |
void spiral(int length){ | |
if (length > 100){ | |
length = 100; | |
} | |
forwardWheels(100,1, 1000); | |
for (int i=1; i <=length; i++){ | |
forwardWheels(100, i, i*200); | |
} | |
sparki.moveStop(); | |
} | |
void veeness(int x, int d, int n, int theta){ | |
int phi = 180 -2*theta; | |
int alpha = 180 - phi; | |
int beta = 90 + theta; | |
int gamma = 90 - theta; | |
for (int i = 0; i < n; i++){ | |
sparki.moveLeft(gamma); | |
sparki.moveForward(x); | |
sparki.moveLeft(alpha); | |
sparki.moveForward(x); | |
sparki.moveRight(beta); | |
sparki.moveForward(d); | |
sparki.moveRight(gamma); | |
sparki.moveForward(x); | |
sparki.moveRight(alpha); | |
sparki.moveForward(x); | |
sparki.moveLeft(beta); | |
sparki.moveForward(d); | |
} | |
stopMotors(); | |
} | |
void abstractLHelper(int x, int d, int n){ | |
for(int i=0; i<n; i++){ | |
sparki.moveLeft(90); | |
sparki.moveForward(x); | |
sparki.moveRight(90); | |
sparki.moveForward(d); | |
sparki.moveRight(90); | |
sparki.moveForward(x); | |
sparki.moveLeft(90); | |
sparki.moveForward(d); | |
} | |
} | |
void abstractL(int x, int d, int n){ | |
abstractLHelper(x, d, n); | |
sparki.moveForward(x+2); | |
sparki.moveLeft(90); | |
sparki.moveForward(d); | |
abstractLHelper(x, d, n); | |
stopMotors(); | |
} | |
}; | |
void writeM(int s){ | |
sparki.moveForward(6*s); | |
sparki.moveRight(90); | |
sparki.moveForward(1*s); | |
sparki.moveRight(45); | |
sparki.moveForward(2.5*s); | |
sparki.moveLeft(90); | |
sparki.moveForward(2.5*s); | |
sparki.moveRight(45); | |
sparki.moveForward(1*s); | |
sparki.moveRight(90); | |
sparki.moveForward(6*s); | |
sparki.moveRight(90); | |
sparki.moveForward(1*s); | |
sparki.moveRight(90); | |
sparki.moveForward(4*s); | |
sparki.moveLeft(135); | |
sparki.moveForward(2.5*s); | |
sparki.moveRight(90); | |
sparki.moveForward(2.5*s); | |
sparki.moveLeft(135); | |
sparki.moveForward(4*s); | |
sparki.moveRight(90); | |
sparki.moveForward(1*s); | |
} | |
void RGBRainbow(){ | |
sparki.RGB(RGB_RED); | |
delay(500); | |
sparki.RGB(RGB_ORANGE); | |
delay(500); | |
sparki.RGB(RGB_YELLOW); | |
delay(500); | |
sparki.RGB(RGB_GREEN); | |
delay(500); | |
sparki.RGB(RGB_BLUE); | |
delay(500); | |
sparki.RGB(RGB_INDIGO); | |
delay(500); | |
sparki.RGB(RGB_VIOLET); | |
delay(500); | |
sparki.RGB(RGB_WHITE); | |
delay(500); | |
sparki.RGB(RGB_OFF); | |
delay(500); | |
} | |
void printBusy(){ | |
sparki.clearLCD(); | |
sparki.println("I'M STILL VERY BUSY"); | |
sparki.updateLCD(); | |
} | |
void printNotBusy(){ | |
sparki.clearLCD(); | |
sparki.println("I'M FREE TO OBEY YOUR COMMAND"); | |
sparki.updateLCD(); | |
} | |
int code; | |
int previousIR; | |
Patterns pattern; | |
void checkIR(){ | |
code = sparki.readIR(); | |
previousIR == code ? code = -1: 0; | |
if(code != -1){ | |
sparki.moveStop(); | |
sparki.gripperStop(); | |
sparki.print("Received code: "); | |
sparki.println(code); | |
sparki.updateLCD(); | |
previousIR = code; | |
} | |
} | |
void setup() { | |
sparki.clearLCD(); | |
} | |
void loop() { | |
printNotBusy(); | |
checkIR(); | |
switch(code){ | |
case 70: sparki.moveForward(); break; | |
case 21: sparki.moveBackward(); break; | |
case 67: sparki.servo(SERVO_RIGHT); sparki.RGB(RGB_BLUE); break; | |
case 71: sparki.moveRight(); break; | |
case 69: sparki.moveLeft(); break; | |
case 68: sparki.servo(SERVO_LEFT); sparki.RGB(RGB_RED); break; | |
case 64: sparki.servo(SERVO_CENTER); sparki.RGB(RGB_GREEN); break; | |
case 9: sparki.gripperOpen(); break; | |
case 7: sparki.gripperClose(); break; | |
case 22: printBusy(); pattern.crazySun(60, 100, 8000, 128000); break; | |
case 25: printBusy(); pattern.squigglyLines(5, 100, 8000, 2); break; | |
case 13: printBusy(); pattern.circle(5, 100, 8000, 24000); break; | |
case 12: printBusy(); pattern.hourglass(20,1); break; | |
case 24: printBusy(); pattern.maze(10,1); break; | |
case 94: printBusy(); pattern.spiral(50); break; | |
case 8: printBusy(); pattern.abstractL(5, 2, 4); break; | |
case 28: printBusy(); pattern.veeness(8, 2, 7, 30); break; | |
case 90: printBusy(); RGBRainbow(); break; | |
case 66: printBusy(); writeM(1); break; | |
case 82: printBusy(); writeM(2); break; | |
case 74: printBusy(); writeM(3);break; | |
default: | |
break; | |
} | |
} | |
// /------^-----\ | |
// | | | |
// | 69 70 71 | | |
// | 68 64 67 | | |
// | 7 21 9 | | |
// | 22 25 13 | | |
// | 12 24 94 | | |
// | 8 28 90 | | |
// | 66 82 74 | | |
// \____________/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment