My Arduino Code Won’t Compile, But it Works on TinkerCad

Hi. I’m doing a project right now and I’m not sure what I’m doing wrong. The code is supposed to make the pitch of the buzzer alter as more or less light is detected from the photo resistor. Each time I upload it says exit status 1, and it says it has an error compiling. Here is my code.

 // C++ code
//
int sensorValue;
int sensorLow = 1023;
int sensorHigh = 0;
int piezoPin = 8;
void setup()
{
  Serial.begin(9600);
  while(millis() < 5000) 
  {
    sensorValue = analogRead(A0);
    if (sensorValue < sensorHigh) 
    {
      sensorHigh = sensorValue;
    }
    if(sensorValue > sensorLow)
    {
      sensorLow = sensorValue;
    }
  }
}
​
void loop()
{
  sensorValue = analogRead(A0);
  int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000);
  tone(piezoPin, pitch, 50);
  Serial.print("pitch: ");
  Serial.println(pitch);
  delay(10);
}
1 Like

Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Here is your code posted properly in code tags and formatted using the IDE autoformat tool. It now compiles for me. There seemed to be an invisible character in line 21 that I deleted.

int sensorValue;
int sensorLow = 1023;
int sensorHigh = 0;
int piezoPin = 8;

void setup()
{
   Serial.begin(9600);
   while (millis() < 5000)
   {
      sensorValue = analogRead(A0);
      if (sensorValue < sensorHigh)
      {
         sensorHigh = sensorValue;
      }
      if (sensorValue > sensorLow)
      {
         sensorLow = sensorValue;
      }
   }
}

void loop()
{
   sensorValue = analogRead(A0);
   int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000);
   tone(piezoPin, pitch, 50);
   Serial.print("pitch: ");
   Serial.println(pitch);
   delay(10);
}
1 Like

I've seen this before. I found its best to copy the code into a very simple text editor, look for odd characters then copy into the IDE.

Hi! Thank you! I didn’t know of the rule before I posted because I was kind of in a rush. Thanks for letting me know! Also, thank you for the tweak in my code. I’ll try it out now.

@coconuttehe, your topic has been moved to a more suitable location on the forum as this has nothing to do with Avrdude, stk500 or Bootloader.

while(millis() < 5000) 

This may not be doing what you expect…

I read the summary and I thought it was related because it said uploading sorry about that.

You have a compile error, not an upload error.

No hard feelings.