I am looking for some coding assistance.
I have a FLORA, and I have written the program below. The idea is that 20 leds light up green, and when button "A" on the Remote is pushed the LED's wipe and only 10 light up Yellow. If you press button "B" the LED's wipe and 5 Light up Red.
The connections are set so that
cPin = 12; (connected from the Flora D12 to the RF D0)
dPin = 6; (Connected from the Flora D6 to the RF D1)
Code: Select all
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_WS2801.h>
//
//debnouncer
//
#define DEBOUNCE 10 // button debouncer, how many ms to debounce, 5+ ms is usually plenty
//
//LEDs
//
int stripDataPin = 10; // Yellow wire on Adafruit Pixels
int stripClockPin = 9; // Green wire on Adafruit Pixels
const int cPin = 12;
const int dPin = 6;
// Set the first variable to the NUMBER of pixels. 32 = 32 pixels in a row
// The LED's in this strip will be 20 per the specifications of the costume
Adafruit_WS2801 strip = Adafruit_WS2801(20,stripDataPin,stripClockPin);
//
//Remote
//
// here is where we define the buttons that we'll use. button "1" is the first, button "6" is the 6th, etc
#define C_PINONREMOTE 1 // was 6
#define D_PINONREMOTE 2 //was 11
byte buttons[] = {C_PINONREMOTE, D_PINONREMOTE}; // the analog 0-5 pins are also known as 14-19
// This handy macro lets us determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)
// we will track if a button is just pressed, just released, or 'currently pressed'
byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];
int start = 0;
int prevX = 0;
int currentX = 0;
int cState = 0;
int dState = 0;
void setup()
{
Serial.begin(9600);
// Start up the LED strip
strip.begin();
// Update the strip, to start they are all 'off'
strip.show();
//pinMode(cPinOnRemote, INPUT);
//pinMode(dPinOnRemote, INPUT);
//What I was looking for was something that would initilize green, powering up one by one.
colorWipe(Color(0, 255, 0), 75); // green fill
}
void check_switches()
{
static byte previousstate[NUMBUTTONS];
static byte currentstate[NUMBUTTONS];
static long lasttime;
byte index;
if (millis() < lasttime) {
// we wrapped around, lets just try again
lasttime = millis();
}
if ((lasttime + DEBOUNCE) > millis()) {
// not enough time has passed to debounce
return;
}
// ok we have waited DEBOUNCE milliseconds, lets reset the timer
lasttime = millis();
for (index = 0; index < NUMBUTTONS; index++) {
justpressed[index] = 0; // when we start, we clear out the "just" indicators
justreleased[index] = 0;
currentstate[index] = digitalRead(buttons[index]); // read the button
/*
Serial.print(index, DEC);
Serial.print(": cstate=");
Serial.print(currentstate[index], DEC);
Serial.print(", pstate=");
Serial.print(previousstate[index], DEC);
Serial.print(", press=");
*/
if (currentstate[index] == previousstate[index]) {
if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
// just pressed
justpressed[index] = 1;
}
else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
// just released
justreleased[index] = 1;
}
pressed[index] = !currentstate[index]; // remember, digital HIGH means NOT pressed
}
//Serial.println(pressed[index], DEC);
previousstate[index] = currentstate[index]; // keep a running tally of the buttons
}
}
void loop()
{
check_switches(); // when we check the switches we'll get the current state
for (byte i = 0; i < NUMBUTTONS; i++) {
if (justpressed[i]) {
Serial.print(i, DEC);
Serial.println(" Just pressed");
// remember, check_switches() will CLEAR the 'just pressed' flag
switch (i)
{
case C_PINONREMOTE:
//do something when var equals 1
// half Power
Serial.println("Half Power");
hideAll();
Half_Power(Color(255,255,0));
break;
case D_PINONREMOTE:
//do something when var equals 2
// Critical_Power
Serial.println("Critical Power");
hideAll();
Critical_Power(Color(255,0,0));
break;
default:
break;
// if nothing else matches, do the default
// default is optional
//
}
}
if (justreleased[i]) {
Serial.print(i, DEC);
Serial.println(" Just released");
// remember, check_switches() will CLEAR the 'just pressed' flag
}
if (pressed[i]){
Serial.print(i, DEC);
Serial.println(" pressed");
// is the button pressed down at this moment
}
}
}
//This most likely does not ever run
// int i = currentX - prevX;
void colorWipe(uint32_t c, uint8_t wait) {
int i;
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
/*void check_switches()
{
cPinOnRemoteState = digitalRead(cPinOnRemote);
dPinOnRemoteState = digitalRead(dPinOnRemote);
//}
//
*/
void Half_Power(uint32_t c){
HalfLight(c);
}
void Critical_Power(uint32_t c){
QuarterLight(c);
}
/* Helper functions */
//Input a value 0 to 384 to get a color value.
//The colors are a transition r - g - b - back to r
void HalfLight(uint32_t c){
for (int i=0; i < 10; i++) {
strip.setPixelColor(i,Color(255, 255, 0));
}
}
void QuarterLight(uint32_t c){
for (int i=0; i < 5; i++) {
strip.setPixelColor(i,Color(255, 0, 0));
}
}
void hideAll(){
for(int i = 0; i > strip.numPixels();i++){
strip.setPixelColor(i,Color(0, 0, 0));
}
strip.show();
}
void displayAll(){
for(int i = 0; i > strip.numPixels();i++){
strip.setPixelColor(i,Color(0, 255, 0));
}
strip.show();
}
// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
uint32_t c;
c = 0;
c <<= 8;
c |= 255;
c <<= 8;
c |= 0;
return c;
}
/* colorWipe(Color(255, 0, 0)); // red fill
colorWipe(Color(255, 255, 0)); // yellow fill
colorWipe(Color(0, 255, 0)); // green fill */