Trying to send a number and receive through Serial Monitor

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

                case 1:
                  tone(9, 261.626);
                  cnt == 0;
                  break;

Did you mean "cnt = 0;" ?

Are you remembering to send a character via serial to your sketch?

no after the tone im setting the counter back to 0

NC_Mechatronics:
no after the tone im setting the counter back to 0

So, yes.

c=0;  //single = for assignment
if (c==0) ; //double = for comparison

NC_Mechatronics:
no after the tone im setting the counter back to 0

Not in the code you posted.

  if (cnt = blinks)

Oops

so change the cnt == 0 to cnt = 0?

but also the serial. read is not working as intended its not picking up any number or inputs from the serial.write(number)

And change the assignment of blinks to cnt to a comparison

in the first if statement i have it generate a random number and then send it through the serial monitor but the serial read on the second if statement that is contained in the first if statement is not receiving the number at all. and i tried isolating both the if statements too and that didnt work.

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)
      {

AWOL:
Not in the code you posted.

  if (cnt = blinks)

Oops

Please post your corrected 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;
              }
            }
          }
        }
      }
    }
  }
}

So your sketch is generating a random number and printing it out on the Serial Monitor and you expect that number to somehow come back into your sketch? That is not how the Serial Monitor works. When your sketch prints things, they are displayed on the Serial Monitor. When you type things into the Serial Monitor and press ENTER, they get sent to your sketch for reading.

Also, you seem confused about '=' vs. '==' The first is assignment, the second is a test for equality so your code

if (cnt = blinks)

is not testing to see if cnt equals blinks, it is assigning the value of blinks to cnt. as AWOL points out, cnt == 0; are useless statements since they are not assignments

Are you aware that numbers over the serial link are ASCII encoded, so '0' has the value 48, '1' has the value 49, and so on?

And Tone does not take floating point numbers, only whole (unsigned int) numbers from 31 to 65535.

AWOL:
Are you aware that numbers over the serial link are ASCII encoded, so '0' has the value 48, '1' has the value 49, and so on?

no i was not. it was not in my lesson.

NC_Mechatronics:
no i was not. it was not in my lesson.

Welcome to the forum, home for all the lessons not taught elsewhere. If you look at the information on "char" type variable, yes it's a signed 8-bit variable, but the common use is ascii, which is the first 128 values, or lower nibble. Whenever you are Serial.read()ing numbers, there will be a few early hurdles you have to overcome, like ascii to int, but also place value, signs etc. I wish you good luck.

Perehama:
but the common use is ascii, which is the first 128 values, or lower nibble.

A nibble is 4 bits, 2 nibbles per byte. ASCII is 7 bits (128).

Or is your definition of a nibble different?

blh64:
is your definition of a nibble different?

No, you are correct. lower 7 bits.

I got it to work as intended by doing a little "surgery" thanks for the help guys