Need help writing a switch/case

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
ZapBranigan
 
Posts: 16
Joined: Fri May 16, 2014 1:27 am

Need help writing a switch/case

Post by ZapBranigan »

Hi all,
I'm trying to write some code that will allow me to selectively activate/deactivate portions of a loop using the "simple RF L4 receiver" and an Arduino Uno R3. My goal is to have four different effects for an LPD8806 strip, each triggered by a button on the four button key fob. The D0, D1, D2, D3 pins on the L4 are connected to the A0, A1, A2, A3 pins on the Uno.

I think that what is required is a statement along the lines of "when pin A0 is HIGH, execute one part of the loop (effect), and ignore all of the rest"
I initially tried to do this using an "if" statement, but couldn't get it quite right (I posted on that a while back, thanks again to those who helped). Over the weekend I tinkered around with a switch/case statement without any success.

I have now exhausted the thimbleful of knowledge that I have (easily done), and could use an assist to get me over the hump! Am I on the right track, or is there a much better way to do this?
Thanks friends!

Code: Select all

#include "LPD8806.h"
#include "SPI.h"

LPD8806 strip = LPD8806(50);

const int switchPinA = A0;
const int switchPinB = A1;
const int switchPinC = A2;
const int switchPinD = A3;

int sensorValueA = 0;
int sensorValueB = 0;
int sensorValueC = 0;
int sensorValueD = 0;


void setup() {
  // Start up the LED strip
  strip.begin();

  // Update the strip, to start they are all 'off'
  strip.show();
  pinMode(switchPinA, INPUT);
  pinMode(switchPinB, INPUT);
  pinMode(switchPinC, INPUT);  
  pinMode(switchPinD, INPUT);
}
// function prototypes, do not remove these!
void colorChase(uint32_t c, uint8_t wait);
void colorWipe(uint32_t c, uint8_t wait);
void dither(uint32_t c, uint8_t wait);
void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait);
void wave(uint32_t c, int cycles, uint8_t wait);
void rainbowCycle(uint8_t wait);
uint32_t Wheel(uint16_t WheelPos);

void loop() {
  sensorValueA = analogRead(switchPinA);
  sensorValueB = analogRead(switchPinB);
  sensorValueC = analogRead(switchPinC);
  sensorValueD = analogRead(switchPinD);
int range = (sensorValueA, sensorValueB, sensorValueC, sensorValueD);
switch (range) {
  case 1: (analogRead(switchPinA) == HIGH);
 // Fill the entire strip with...

colorWipe(strip.Color(127,1,25), 10);       //pink

  
  // Clear strip data before start of next effect
  for (int i=0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, 0);
  }
  break;
  
  case 2: (analogRead(switchPinB) == HIGH);
 // Fill the entire strip with...
colorWipe(strip.Color(127,18,0), 10);       //orangered
  
  // Clear strip data before start of next effect
  for (int i=0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, 0);
  }
  break;
  
  case 3: (analogRead(switchPinC) == HIGH);

 colorWipe(strip.Color(127, 0, 127), 10);  //magenta
  
  // Clear strip data before start of next effect
  for (int i=0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, 0);
  } 
  break;
  
  case 4: (analogRead(switchPinD) == HIGH);

colorWipe(strip.Color(0,0,127), 10);      // blue

  
  // Clear strip data before start of next effect
  for (int i=0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, 0);
  }  
  break;
}
}

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);
      }

    }
  

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Need help writing a switch/case

Post by Franklin97355 »

Put some Serial.prints in your code here

Code: Select all

void loop() {
  sensorValueA = analogRead(switchPinA);
  sensorValueB = analogRead(switchPinB);
  sensorValueC = analogRead(switchPinC);
  sensorValueD = analogRead(switchPinD);
And see if the values are what you are expecting.

User avatar
ZapBranigan
 
Posts: 16
Joined: Fri May 16, 2014 1:27 am

Re: Need help writing a switch/case

Post by ZapBranigan »

Franklin,
Thanks for the suggestion. I added some serial print code:

Code: Select all

#include "LPD8806.h"
#include "SPI.h"

LPD8806 strip = LPD8806(50);

const int switchPinA = 2;
const int switchPinB = 3;
const int switchPinC = 4;
const int switchPinD = 5;

int sensorValueA = 0;
int sensorValueB = 0;
int sensorValueC = 0;
int sensorValueD = 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(switchPinA, INPUT);
  pinMode(switchPinB, INPUT);
  pinMode(switchPinC, INPUT);  
  pinMode(switchPinD, INPUT);
}
// function prototypes, do not remove these!
void colorChase(uint32_t c, uint8_t wait);
void colorWipe(uint32_t c, uint8_t wait);
void dither(uint32_t c, uint8_t wait);
void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait);
void wave(uint32_t c, int cycles, uint8_t wait);
void rainbowCycle(uint8_t wait);
uint32_t Wheel(uint16_t WheelPos);

void loop() {
  sensorValueA = digitalRead(switchPinA);
  sensorValueB = digitalRead(switchPinB);
  sensorValueC = digitalRead(switchPinC);
  sensorValueD = digitalRead(switchPinD);
  Serial.print(sensorValueA);
  Serial.print(sensorValueB);
  Serial.print(sensorValueC);
  Serial.print(sensorValueD);
int range = (sensorValueA, sensorValueB, sensorValueC, sensorValueD);
switch (range) {
  case 1: (digitalRead(switchPinA) == HIGH);
 // Fill the entire strip with...

colorWipe(strip.Color(127,1,25), 10);       //pink

  
  // Clear strip data before start of next effect
  for (int i=0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, 0);
  }
  break;
  
  case 2: (digitalRead(switchPinB) == HIGH);
 // Fill the entire strip with...
colorWipe(strip.Color(127,18,0), 10);       //orangered
  
  // Clear strip data before start of next effect
  for (int i=0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, 0);
  }
  break;
  
  case 3: (digitalRead(switchPinC) == HIGH);

 colorWipe(strip.Color(127, 0, 127), 10);  //magenta
  
  // Clear strip data before start of next effect
  for (int i=0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, 0);
  } 
  break;
  
  case 4: (digitalRead(switchPinD) == HIGH);

colorWipe(strip.Color(0,0,127), 10);      // blue

  
  // Clear strip data before start of next effect
  for (int i=0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, 0);
  }  
  break;
}
}

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);
      }

    }
  
I'm not sure what to make of what shows up on my serial monitor. Here is a screen shot:
Screen Shot 2014-08-19 at 5.32.05 PM.png
Screen Shot 2014-08-19 at 5.32.05 PM.png (142.33 KiB) Viewed 129 times
I think this indicates that one of the four pins is HIGH, which is what think should happen. The loop is not behaving correctly, however. I'm going to play around with it. I'll post if I figure anything out. Thanks again.

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Need help writing a switch/case

Post by Franklin97355 »

Make that last Serial.print a Serial.println and it will scroll down your screen

User avatar
ZapBranigan
 
Posts: 16
Joined: Fri May 16, 2014 1:27 am

Re: Need help writing a switch/case

Post by ZapBranigan »

Thanks. So I did that, and I'm getting pretty much the expected results from the L4 connected pins. Here is a screenshot, the input pins seem to be returning either 0 or 1 depending upon the button pressed on the key fob.
Screen Shot 2014-08-20 at 7.36.40 PM.png
Screen Shot 2014-08-20 at 7.36.40 PM.png (133.98 KiB) Viewed 103 times
Unfortunately the light effects on the LPD8806 are not quite what i'm trying to achieve. When I press button A, the pink color wipe begins, all good so far. However, when I hit any other button, the LED strip goes to solid pink, and does not launch a color wipe of a different color as I intended in the code. More tinkering clearly required.

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”