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
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
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.
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
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!
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
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..
The arduino program marks the top line as having an error " void loop() {".
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.