trying to use a keypad to fire off functions, and I just can't seem to get it to work, I can can get the input to print to the serial monitor without the switch statement
all the functions work
as soon as I try putting a switch statement in nothing works....help
#define redPin 10
#define greenPin 11
#define bluePin 12
#include <Keypad.h>
int redPulse = 0;
int greenPulse = 0;
int bluePulse = 0;
int sensorPin = A2;
int sensorPin1 = A3;
int sensorValue = 0;
int sensorValue1 = 0;
int w;
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'#', '0', '*', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
randomSeed(analogRead(0));
Serial.begin(9600);
}
void loop(){
char key = keypad.getKey();
l1: if (key) // Check for a valid key.
{
switch(key)
{
case '1':
randomcolor();
break;
case '2':
mainColors();
break;
case '3':
showSpectrum();
break;
case'4':
w=4;
break;
case '5':
w=5;
break;
case '6':
w=6;
break;
case '7':
w=7;
break;
case '8':
w=8;
break;
case '9':
w=9;
break;
}
}
else { goto l1;} // To guarantee that a key is pressed before moving on with the rest of code
}
void randomcolor() {
redPulse = random(0, 256); // Randomly generate 1 to 255
greenPulse = random(0, 256); // Randomly generate 1 to 255
bluePulse = random(0, 256); // Randomly generate 1 to 255
analogWrite(redPin, redPulse);
analogWrite(greenPin, greenPulse);
analogWrite(bluePin, bluePulse);
for (redPulse = 0; redPulse < 255; redPulse += 1) {
analogWrite(redPin, redPulse);
analogWrite(greenPin, greenPulse);
analogWrite(bluePin, bluePulse);
sensorValue = analogRead(sensorPin);
setdelay();
Serial.println(sensorValue);
}
for (redPulse = 255; redPulse > 0; redPulse -= 1) {
analogWrite(redPin, redPulse);
analogWrite(greenPin, greenPulse);
analogWrite(bluePin, bluePulse);
sensorValue = analogRead(sensorPin);
setdelay();
//rial.println(sensorValue);
}
for (greenPulse = 0; greenPulse < 255; greenPulse += 1) {
analogWrite(redPin, redPulse);
analogWrite(greenPin, greenPulse);
analogWrite(bluePin, bluePulse);
sensorValue = analogRead(sensorPin);
setdelay();
//Serial.println(sensorValue);
}
for (greenPulse = 255; greenPulse > 0; greenPulse -= 1) {
analogWrite(redPin, redPulse);
analogWrite(greenPin, greenPulse);
analogWrite(bluePin, bluePulse);
sensorValue = analogRead(sensorPin);
setdelay();
//Serial.println(sensorValue);
}
for (bluePulse = 0; bluePulse < 255; bluePulse += 1) {
analogWrite(redPin, redPulse);
analogWrite(greenPin, greenPulse);
analogWrite(bluePin, bluePulse);
sensorValue = analogRead(sensorPin);
setdelay();
//Serial.println(sensorValue);
}
for (bluePulse = 255; bluePulse > 0; bluePulse -= 1) {
analogWrite(redPin, redPulse);
analogWrite(greenPin, greenPulse);
analogWrite(bluePin, bluePulse);
sensorValue = analogRead(sensorPin);
setdelay();
//Serial.println(sensorValue);
}
}
void mainColors()
{
// Off (all LEDs off):
//digitalWrite(redPin, LOW);
//digitalWrite(greenPin, LOW);
//digitalWrite(bluePin, LOW);
//sensorValue = analogRead(sensorPin)*2;
//delay(sensorValue);
// Red (turn just the red LED on):
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
sensorValue = analogRead(sensorPin) * 2;
delay(sensorValue);
// Green (turn just the green LED on):
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, LOW);
sensorValue = analogRead(sensorPin) * 2;
delay(sensorValue);
// Blue (turn just the blue LED on):
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);
sensorValue = analogRead(sensorPin) * 2;
delay(sensorValue);
// Yellow (turn red and green on):
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, LOW);
sensorValue = analogRead(sensorPin) * 2;
delay(sensorValue);
// Cyan (turn green and blue on):
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, HIGH);
sensorValue = analogRead(sensorPin) * 2;
delay(sensorValue);
// Purple (turn red and blue on):
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);
sensorValue = analogRead(sensorPin) * 2;
delay(sensorValue);
// White (turn all the LEDs on):
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, HIGH);
sensorValue = analogRead(sensorPin) * 2;
delay(sensorValue);
}
void showSpectrum()
{
int x; // define an integer variable called "x"
// Now we'll use a for() loop to make x count from 0 to 767
// (Note that there's no semicolon after this line!
// That's because the for() loop will repeat the next
// "statement", which in this case is everything within
// the following brackets {} )
for (x = 0; x < 768; x++)
// Each time we loop (with a new value of x), do the following:
{
showRGB(x); // Call RGBspectrum() with our new x
sensorValue = analogRead(sensorPin);
delay(sensorValue);
}
}
// showRGB()
// This function translates a number between 0 and 767 into a
// specific color on the RGB LED. If you have this number count
// through the whole range (0 to 767), the LED will smoothly
// change color through the entire spectrum.
// The "base" numbers are:
// 0 = pure red
// 255 = pure green
// 511 = pure blue
// 767 = pure red (again)
// Numbers between the above colors will create blends. For
// example, 640 is midway between 512 (pure blue) and 767
// (pure red). It will give you a 50/50 mix of blue and red,
// resulting in purple.
// If you count up from 0 to 767 and pass that number to this
// function, the LED will smoothly fade between all the colors.
// (Because it starts and ends on pure red, you can start over
// at 0 without any break in the spectrum).
void showRGB(int color)
{
int redIntensity;
int greenIntensity;
int blueIntensity;
// Here we'll use an "if / else" statement to determine which
// of the three (R,G,B) zones x falls into. Each of these zones
// spans 255 because analogWrite() wants a number from 0 to 255.
// In each of these zones, we'll calculate the brightness
// for each of the red, green, and blue LEDs within the RGB LED.
if (color <= 255) // zone 1
{
redIntensity = 255 - color; // red goes from on to off
greenIntensity = color; // green goes from off to on
blueIntensity = 0; // blue is always off
}
else if (color <= 511) // zone 2
{
redIntensity = 0; // red is always off
greenIntensity = 255 - (color - 256); // green on to off
blueIntensity = (color - 256); // blue off to on
}
else // color >= 512 // zone 3
{
redIntensity = (color - 512); // red off to on
greenIntensity = 0; // green is always off
blueIntensity = 255 - (color - 512); // blue on to off
}
// Now that the brightness values have been set, command the LED
// to those values
analogWrite(redPin, redIntensity);
analogWrite(bluePin, blueIntensity);
analogWrite(greenPin, greenIntensity);
}
void setdelay()
{
if (sensorValue < 720)
{
delay(250);
}
else if (sensorValue >= 750 || sensorValue > 780)
{
delay(500);
}
else if (sensorValue >= 750 || sensorValue > 780)
{
delay(750);
}
else if (sensorValue >= 780 || sensorValue > 810)
{
delay(1000);
}
else if (sensorValue >= 810 || sensorValue > 840)
{
delay(1250);
}
else if (sensorValue >= 840 || sensorValue > 870)
{
delay(1500);
}
else if (sensorValue >= 870 || sensorValue > 900)
{
delay(1750);
}
else if (sensorValue >= 900 || sensorValue > 930)
{
delay(2000);
}
else if (sensorValue > 930)
{
delay(2125);
}
}