Random # Generator/LED Blink/Buzzer Sound

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

}

A rather contrived exercise!

Given your use of delay, you have painted yourself into a corner where you have no choice but to use interrupts to detect the LED.

If the use of interrupts has not come up in class yet, it may be that your teacher intended you to use millis for timing the on/off of the LED and that will let you poll to get the number of times the photoresistor sees the LED.

Nice presentation and code supplied. Please use autoformat in the IDE, Ctrl + T, mark the code, click the </> symbol here, (code tags) and paste the code.

Study the topics "How to do several things at the same time" and "How to use millis. From them You have lots to gain.

Hello and good morning
Well I guess the number of 38 delay()'s will disturb the requsted processing.
Take a view to this built-In example and modify this example to your requirements.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.