Skip to content

Instantly share code, notes, and snippets.

Created February 4, 2012 23:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/1740953 to your computer and use it in GitHub Desktop.
Save anonymous/1740953 to your computer and use it in GitHub Desktop.
File to drive Rainbowduino from simple serial interface
/*
Rainbowduino v3.0 Library examples:
Sets pixels on 2D plane (8x8 matrix)
*/
#include <Rainbowduino.h>
uint32_t colorRGB[13] = {0xFFFFFF,0x000000,0xFFFFFF,0x000,0xFF0000,0x00FF00,0x0000FF,0xFF0000,0x00FF00,0x0000FF,0xFF0000,0x00FF00,0x0000FF };
uint32_t colourwhite = 0xFFFFFF;
uint32_t colourgrey = 0x7F7F7F;
uint32_t colourcharcoal = 0x3F3F3F;
// uint32_t colourbrick = 0x1f0F00;
// uint32_t colourroof = 0x2f0F00;
uint32_t colourbrick = 0x1f1f1f;
uint32_t colourroof = 0x1f1f1f;
uint32_t colour0 = 0x0f0F00;
uint32_t colour1 = 0x1f0F00;
uint32_t colour2 = 0x2f0F00;
uint32_t colour3 = 0x3f0F00;
uint32_t colour4 = 0x4f0F00;
uint32_t colour5 = 0x5f0F00;
uint32_t colour6 = 0x6f0F00;
uint32_t colour7 = 0x7f0F00;
unsigned int colour_red = 0;
unsigned int colour_green= 0;
unsigned int colour_blue = 0;
uint32_t colour_palette = 0 ;
// int inByte = 0; // incoming serial byte
unsigned char x,y,z;
long previousMillis = 0; // will store last time LED was updated
long interval = 100; // interval at which to blink (milliseconds)
int wattsDesired = 0000;
int wattsNearest = 0;
int incomingByte = 0;
boolean dataReady = false;
int i = 0;
int len = 4; // expected string is 6 bytes long
char inString[5];
char inByte;
char inChar;
int incoming;
int PWMpin = 3; // PWM output on pin 3 to MPPT active load
int PWMval = 0;
int LEDpin = 5; // PWM drive to LED driver
int LEDval = 0;
int FANpin = 6; // PWM to fan speed control
int FANval = 0;
int UPpin = 8; // Pins for UP and DOWN switches
int DOWNpin = 9;
int allLamps = B00000000;
byte led1 = 2;
byte led2 = 3;
byte led3 = 4;
byte led4 = 5;
byte led5 = 6;
byte led6 = 7;
byte led7 = 8;
byte led8 = 9;
byte led[] = {
led1, led2, led3, led4, led5, led6, led7, led8};
byte activeLED = 0;
int temp = 0;
int temp2 = 0;
void setup()
{
Rb.init();
Serial.begin(9600); // start serial port at 9600 bps:
// establishContact(); // send a byte to establish contact until receiver responds
// Draw the house
Rb.drawRectangle(0, 0, 6, 8, colourbrick); // draw a filled rectangle line from (0,0) of length and width 7 pixels
Rb.setPixelXY(6,1,colourroof); //uses R, G and B bytes
// Rb.setPixelXY(6,2,(0xff),(0xff),(0xFF)); //uses R, G and B bytes
// Rb.setPixelXY(6,3,(0xff),(0xff),(0xFF)); //uses R, G and B bytes
// Rb.setPixelXY(6,4,(0xff),(0xff),(0xFF)); //uses R, G and B bytes
// Rb.setPixelXY(6,5,(0xff),(0xff),(0xFF)); //uses R, G and B bytes
Rb.setPixelXY(6,6,colourroof); //uses R, G and B bytes
// Rb.setPixelXY(7,1,colourcharcoal); //uses R, G and B bytes
Rb.setPixelXY(7,2,colourroof); //uses R, G and B bytes
Rb.setPixelXY(7,3,colourroof); //uses R, G and B bytes
Rb.setPixelXY(7,4,colourroof); //uses R, G and B bytes
Rb.setPixelXY(7,5,colourroof); //uses R, G and B bytes
// Rb.setPixelXY(7,6,colourcharcoal); //uses R, G and B bytes
delay(1000);
Serial.print("Enter Command A-F and number 0 - 255: ");
}
void loop()
{
colour_red= 0;
colour_blue=0;
colour_green=0;
serialReader();
if (dataReady = true) {
wattsDesired = incoming;
}
display_colour();
} //End of loop
int display_colour()
{
int n=(incoming+256)*30/27;
if(n<=255) {colour_blue = 255 -n; colour_green = n;}
else { colour_blue = 0;}
// if(n<=256) {colour_green = n;}
// else {colour_green = 0;}
if(n>=256) {colour_red = n - 256; colour_green = 511 -(n);}
else {colour_red = 0;}
if(n==0) { colour_green = 0; colour_red = 0;}
colour_palette = ((65536 * colour_red) + (256*colour_green) + colour_blue);
Serial.print (colour_red);
Serial.print (" ");
Serial.print (colour_green);
Serial.print (" ");
Serial.print (colour_blue);
Serial.print (" ");
Serial.println (colour_palette);
colour_palette = ((65536 * colour_red) + (256*colour_green) + colour_blue);
Rb.fillRectangle(1, 1, 4, 6, colour_palette); // draw a filled rectangle line from (0,0) of length and width 7 pixels
}
void writeOut () {
temp = 0;
byte i = 0;
byte mask = 0x01;
while (i < 8) {
mask = 0x01 << i;
if (allLamps & mask) {
digitalWrite(led[i], HIGH);
}
else {
digitalWrite(led[i], LOW);
}
i++;
}
}
int serialReader(){
if (Serial.available() > 0) { // if there are bytes waiting on the serial port
inByte = Serial.read(); // read a byte
delay(10); // *** added to make it work ***
if (inByte == 13){ // follow the false code until you get a CR
dataReady = true;
inChar = inString[1]; // get the leading character
inString[1] = 48; // clear the leading character
temp = atoi(inString); // convert ascii to integer
incoming = temp;
if (temp > 255) temp = 255;
/*
Serial.println(inString[0]);
Serial.println(inString[1]);
Serial.println(inString[2]);
Serial.println(inString[3]);
Serial.println(inString[4]);
Serial.println(inString[5]);
*/
// Action Routines: Work out the command, the argument and what to do with it
// Echo the command (verbose form) and value to serial terminal
switch (inChar){ //
case 97:
Serial.print (" Active Load ");
PWMval = temp;
analogWrite(PWMpin, PWMval); // Update the PWM pin
break;
case 98:
Serial.print (" Brightness ");
LEDval = temp;
analogWrite(LEDpin, LEDval); // Update the LED brightness
break;
case 99:
Serial.print (" Current ");
break;
case 100:
Serial.print (" Distance ");
break;
case 101:
Serial.print (" Echo ");
break;
case 102:
Serial.print (" Fan Speed ");
FANval = temp;
analogWrite(FANpin, FANval); // Update the fan speed
break;
}
Serial.println(temp);
inString[0] = 0;
inString[1] = 0;
inString[2] = 0;
inString[3] = 0;
inString[4] = 0;
i = 0;
return incoming;
}
else dataReady = false; // No CR seen yet so put digits into array
// Serial.print(inByte);
inString[i] = inByte;
i++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment