I am trying to send a randomly generated number through the serial monitor and have it read and blink an led a certain amount of times and i cant seem to get it to work.
here is my code
int button = 2; // set button pin to pin 2
int led = 8; // set led pin to pin 8
int buzzer = 9; // set buzzer pin to pin 9
int photoresistor = A0; // set photoresistor pin to analog pin 0
int cnt = 0; // set up a counter
int incomingbyte = 0; // set up an incoming signal
void setup() {
// put your setup code here, to run once:
pinMode(button, INPUT); // set button as input
pinMode(led, OUTPUT); // set led as output
pinMode(buzzer, OUTPUT); // set buzzer as output
Serial.begin(9600); // start serial monitoring
}
void loop() {
// put your main code here, to run repeatedly:
int buttonstate = digitalRead(button); // set an integer equal to the buttons state
if (buttonstate == HIGH) // if buttonstate is high than generate a random number
{
int number = random(1, 5);
delay(1000); // ghetto debounce
Serial.println(number); // print the random generated number in the serial monitor
if (Serial.available())
{
char blinks = Serial.read();
Serial.println(blinks);
if (blinks > 0)
{
for (int x = 0; x < blinks; x++)
{
int analogValue = analogRead(photoresistor);
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
if ( analogValue > 50)
{
cnt++;
if (cnt = blinks)
{
switch (blinks)
{
case 1:
tone(9, 261.626);
cnt == 0;
break;
case 2:
tone(9, 293.665);
cnt == 0;
break;
case 3:
tone(9, 329.628);
cnt == 0;
break;
case 4:
tone(9, 349.228);
cnt == 0;
break;
case 5:
tone(9, 391.995);
cnt == 0;
break;
default:
cnt == 0;
break;
}
}
}
}
}
}
}
}