Hi guys and gals.
My first post here, but I have enjoyed looking at others as they have helped previously. I have a school project where I am supposed to get a LED to blink a corresponding number of times to a randomly generated number. I've got that part successfully, but I am unable to complete the secondary portion: use a photoresistor to sense the light from the LED and sound a buzzer the number of times the LED blinks. This all has to be done with seperate coding, i.e. the light and the buzzer cannot both operate off the random number generator. Below is my code. Any assistance would be wonderful. Thank you!
int LSensor = A0;
int LED = 13;
int Piezo = 6;
int value;
boolean go = false;
int ambient = 1010;
int photoRead = 0;
void setup()
{Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(LED, OUTPUT);
pinMode(LSensor, INPUT);
pinMode(Piezo, OUTPUT);
}
void loop(){
int number = random(2, 7);
Serial.println(number);
delay(5500);
digitalWrite(LED, !digitalRead(LED)); // deleted in official to test
rand;
switch(number)
{
case 1 : digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
break;
case 2 : digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
delay(250);
digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
break;
case 3 : digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
delay(250);
digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
delay(250);
digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
break;
case 4 : digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
delay(250);
digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
delay(250);
digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
delay(250);
digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
break;
case 5 : digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
delay(250);
digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
delay(250);
digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
delay(250);
digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
delay(250);
digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
break;
case 6 : digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
delay(250);
digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
delay(250);
digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
delay(250);
digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
delay(250);
digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
delay(250);
digitalWrite (LED, HIGH);
delay(500);
digitalWrite (LED, LOW);
break;
}
int LSensorStatus = analogRead(LSensor);
if (LSensorStatus >= 100) {
tone(Piezo, 100, 100);
delay(100);
}
else {
noTone(Piezo);
}
}