Sound Performance Toy

video: Here

Code is Collapsed Here:

[sourcecode collapse=”true”]
<pre>int speakerPin= 7;
int ledPin = 5;
int photoPin = A5;
int potPin = A0;
int var;
int potDelay;
int photoDelay;
int photoVal; //analogRead of the photocell //set by
int potVal; //analogRead of the potentiometer

int newphotoVal;

int toneVal; //set by photo, pot, pot, photo (case 1 -4)

int switch1 = 11; //red cord
int switch2 = 12; //orange cord

int newSwitch1;
int newSwitch2;

void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);

}

void loop()
{
newSwitch1 = digitalRead(switch1);

newSwitch2 = digitalRead(switch2);

photoVal = analogRead(photoPin);
potVal = analogRead(potPin);

potDelay = map(potVal, 0, 1023, 0, 250);
photoDelay = map(photoVal, 0, 1023, 0, 250);

//1st case
if (newSwitch1 == 1 && newSwitch2 == 1)
{
var = 1;
}
//2nd case
if (newSwitch1 ==0 && newSwitch2 == 1)
{
var = 2;
}

if(newSwitch1 == 1 && newSwitch2 == 0)
{
var = 3;
}

if(newSwitch1 == 0 && newSwitch2 == 0)
{
var = 4;
}

switch(var)
{
case 1:
potToLEDphotoToSpeaker(); //case 1
break;

case 2:
photoToLEDpotToSpeaker(); //case 2
break;

case 3:
potToLEDblinkAndSpeaker();
break;

case 4:
photoToLEDblinkAndSpeaker();
break;

}
}

void photoToLEDblinkAndSpeaker()
{
//case 4
analogWrite(ledPin, HIGH); //analogWrite(pin, value)
delay(photoDelay);
analogWrite(ledPin,LOW);
delay(photoDelay);

tone(speakerPin, photoVal*2); //instead of *4
delay(photoDelay);
noTone(speakerPin);
}

void photoToLEDpotToSpeaker()
{
//case 2
analogWrite(ledPin, (photoVal/4));
tone(speakerPin, potVal*4);
delay(20);
}

void potToLEDblinkAndSpeaker()
{
//case 3
analogWrite(ledPin, HIGH); //analogWrite(pin, value)
delay(potDelay);
analogWrite(ledPin,LOW);
delay(potDelay);

tone(speakerPin, potVal*4);
delay(potDelay);
noTone(speakerPin);
}

void potToLEDphotoToSpeaker()
{
//case 1
//pot to LED and photo to speaker
analogWrite(ledPin, (potVal/4));
tone(speakerPin, photoVal);
delay(20);
}

[/sourcecode]

Leave a Reply

Discover more from Making Toys

Subscribe now to keep reading and get access to the full archive.

Continue reading