My first program.

Hi there!
I recently purchased my first Arduino, so i am completely fresh at programming.
I am now on my first project in the starter kit(Light Theremin) and i’ve stumbled upon my first problem.
I’ve tried adding } and { some places, but i dont seem to understand what the problem is.
All help will be greatfully appriciated :slight_smile:

My program:

int sensorValue;
int sensorLow = 1023;
int sensorMax = 0;
const int ledPin = 13;

void setup() {
  
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  
  while (millis() < 5000) {
    
    sensorValue = analogRead(A0);
    if (sensorValue > sensorHigh) {
      sensorHigh = sensorValue;
    }
    if (sensorValue < sensorLow) {
      sensorLow = sensorValue;
    }
  }
  
  void loop(13) {
    sensorValue = analogRead(A0);
    
    int pitch =
    map(sensorValue,sensorLow,sensorHigh, 50, 4000);
    
    tone(8,pitch,20);
    
    delay(10);
  }

Here are the errors i recive:

_1prog:4: error: variable or field 'loop' declared void
_1prog:3: error: expected unqualified-id before '{' token
_1prog.ino: In function 'void setup()':
_1prog:8: error: 'ledPin' was not declared in this scope
_1prog:14: error: 'sensorHigh' was not declared in this scope
_1prog:22: error: variable or field 'loop' declared void
_1prog:31: error: expected `}' at end of input
  void loop(13) {

The loop function doesn't take any arguments. If it did, you'd need to define the type of the variable. If you did, variable names can not start with numbers.

Use Tools + Auto Format to properly indent code. That mess is too hard to read.

You presumably had something in mind when using
void loop(13)What was it ?

I just forgot to edit it out before posting, one of my attempts to get this to work..
As i said, it's my 1st program so i dont have much of an idea what i am doing.
So even tho i remove the (13) i still get error further down.

_1prog.ino: In function 'void setup()':
_1prog:13: error: 'sensorHigh' was not declared in this scope
_1prog:21: error: a function-definition is not allowed here before '{' token
_1prog:30: error: expected `}' at end of input

_1prog:13: error: ‘sensorHigh’ was not declared in this scope = in change in the scope “sensorMax” to “sensorHigh”

void setup() {

while (millis() < 5000) {

if (sensorValue > sensorHigh) { }
if (sensorValue < sensorLow) { }

}

→ missing brace at this point }

The code is also missing a closing bracket ("}") at the end of setup. You'll note an error message if you attempt to format your code (Control-T).

BTW I believe that sensorHigh is used in two places. So you'll need to fix it in both lines.

Regards,

Brad
KF7FER

I think that is my remaining problem, i am totaly new to coding and i guess this is a pretty simple fix.
So if anyone could tell me where and maybe why i get the error. I would be very greatfull! :slight_smile:

int sensorValue;
int sensorLow = 1023;
int sensorHigh = 0;
const int ledPin = 13;

void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
  
  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(8,pitch,20);
    
    delay(10);
  }
_2prog.ino: In function 'void setup()':
_2prog:21: error: a function-definition is not allowed here before '{' token
_2prog:29: error: expected `}' at end of input

In setup() you have a { after WHILE but you have no matching }

Get into the habit of writing the closing bracket immediately after you write the opening bracket and before you put any stuff between them.

And also adopt a standard system for indenting your code and stick to it. It will then become easier to see where things are missing.

…R

You need a bracket '}' before the 1st line of the loop function:

}


  void loop()   {

luisilva:
You need a bracket '}' before the 1st line of the loop function:

That's the same bracket that I mentioned - just so the OP knows there aren't two problems.

...R

Well, as i am stating. This is my first ever program, i've been trying to watch instructional videos etc. But i am somehow struggeling to understand how the diffrent brackets work and how they need to be set up. I get that every { bracket needs an } to end it. But i don't know where to put them. I am currently diong the Arduino starter projects and trying to learn the coding as i go along, so i am basically just whriting of from the book, even then i stumble into problems...

I've tried to correct the error placing } brackets before the loop. But as i am a total noob, i guess i placed it wrong when the same error keeps occuring. Sorry for asking stupid quesions.. :cold_sweat:

The arduino program marks the top line as having an error " void loop() {".

}
                  
                              
  void loop()   {
    sensorValue = analogRead(A0); 
    int pitch =
    map(sensorValue,sensorLow,sensorHigh, 50, 4000);
    
    tone(8,pitch,20);
    
    delay(10);
  }

aspp:
Well, as i am stating. This is my first ever program, i've been trying to watch instructional videos etc.

Yes we get it, you don't have to be reminded with every post. Post your entire sketch, not just snippets.

If the program is only what you show, you don’t need the first bracket ‘}’.

The purpose of the brackets is only define blocks of code. One function needs to be a block of code. If you want to repeat more than one line the lines that you are going to repeat are a block of code. One example of that is what you do after the line

while (millis() < 5000)

After this line you begin a block of code whit ‘{’ that will be repeated. After a if, after other conditional test structures, like switch() you will need to add an ‘{’. You can have blocks inside blocks (i.e., you can have ‘{’ and the another ‘{’, but you always have to close the brackets).

int sensorValue;
int sensorLow = 1023;
int sensorHigh = 0;
const int ledPin = 13;

void setup() {   // <- begin of one block for the FUNCTION (1)
   pinMode(ledPin, OUTPUT);
   digitalWrite(ledPin, HIGH);

   while (millis() < 5000) {   // <- begin of one block that will be repeated for the WHILE (2)

      sensorValue = analogRead(A0);

      if (sensorValue > sensorHigh) {   // <- begin of other block for the IF (3)
         sensorHigh = sensorValue;
      }   // <- end of the first block of the IF (3)
      if (sensorValue < sensorLow) {   // <- begin of other block for the IF (4)
         sensorLow = sensorValue;
      }   // <- end of the block of the IF (4)
   }   // <- end of the block of the WHILE (2)
} //<- you miss this one that is the end of the last block (the block of the FUNCTION) (1)

void loop()   {
   sensorValue = analogRead(A0); 
   int pitch =map(sensorValue,sensorLow,sensorHigh, 50, 4000);

   tone(8,pitch,20);

   delay(10);
}   // in the case of this function you only have one block

aspp:
Well, as i am stating. This is my first ever program, i've been trying to watch instructional videos etc.

Study several of the examples that come with the Arduino IDE - they should work "out of the box" so there is no risk of you accidentally introducing errors while typing.

Then make a copy of some of the examples and try modifying the code. If you start getting errors you can always go back to the original and start over.

...R