Switch case sound performance toy…work in progress
So, I am completely confused by the wires to switch states…..What I have now grabs each case and will use the speaker and the LED in the exact state its in when I press a number….
int speakerPin= 7;
int ledPin = 3;
int sensorPin = 5;
int potPin = 0;
int sensorVal;
int potVal;
int LEDbrightness;
int toneChange;
int sensorTime;
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
if ( Serial.available()) // Check to see if at least one character is available
{
char ch = Serial.read();
switch(ch) {
case ‘1’:
//1st State controls LED dim level with the POT, and Speaker with the the PhotoCell
sensorVal = analogRead(sensorPin);
potVal = analogRead(potPin);
LEDbrightness = map(potVal, 0, 1023, 0, 255);
analogWrite(ledPin, LEDbrightness);
analogWrite(ledPin, (potVal/4));
tone(speakerPin, sensorVal*4);
// Serial.print(sensorVal);
// Serial.print(” “);
// Serial.println(potVal);
delay(20);
break;
case ‘2’:
//*2nd State controls LED dim level with Photocell, and Speaker Tone with POT
sensorVal = analogRead(sensorPin);
potVal = analogRead(potPin);
// sensorVal = 1023 – sensorVal;
LEDbrightness = map(sensorVal, 0, 1023, 0, 255);
analogWrite(ledPin, LEDbrightness);
toneChange =map(potVal, 0, 1023, 0, 255);
tone(speakerPin, toneChange);
Serial.print(sensorVal);
Serial.print(” “);
Serial.println(potVal);
delay(20);
break;
case ‘3’:
//*3rd State controls LED blink-rate with the POT, Speaker Tone with POT, and Delay Rate (for both) with PhotoCell
sensorTime = ((sensorVal)/10);
Serial.println(“sensor reading = “);
Serial.println(sensorVal); // the raw analog reading
Serial.println(” “);
Serial.println(“TIME reading = “);
Serial.print(sensorTime);
//Serial.print(” “);
// Serial.println(potVal);
sensorVal = analogRead(sensorPin);
potVal = analogRead(potPin);
LEDbrightness = map(potVal, 0, 1023, 0, 255);
analogWrite(ledPin, LEDbrightness);
// toneChange =map(potVal, 0, 1023, 0, 255);
// tone(speakerPin, toneChange);
//
// Serial.print(sensorVal);
// Serial.print(” “);
// Serial.println(potVal);
//
// delay(sensorTime);
delay(sensorTime);
digitalWrite(ledPin, LOW);
delay(sensorTime);
break;
case ‘4’:
// *4rd State controls LED blink-rate with the PhotoCell, and Speaker tone with PhotoCell.
sensorTime = ((sensorVal)/10);
sensorVal = analogRead(sensorPin);
analogWrite(ledPin, (potVal/4));
tone(speakerPin, sensorVal*4);
digitalWrite(ledPin,HIGH);
delay(sensorTime);
digitalWrite(ledPin, LOW);
delay(sensorTime);
break;
default :
Serial.print(ch);
Serial.println(” was received but not expected”);
break;
}//switch
} //serial available
} //void loop
int RCtime(int RCpin) {
int reading=0; //startwith0
// set the pin to an output and pull to LOW (ground)
pinMode(RCpin, OUTPUT);
digitalWrite(RCpin, LOW);
// Now set the pin to an input and…
pinMode(RCpin, INPUT);
while (digitalRead(RCpin) == LOW) { // count how long it takes to rise up to HIGH
reading++; // increment to keep track of time
if (reading == 30000) {
// if we got this far, the resistance is so high // its likely that nothing is connected!
break; // leave the loop
} }
// OK either we maxed out at 30000 or hopefully got a reading, return the count
return reading; }
Reply