Help with my project Plz

Does someone see something wrong with my code :

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;
digitalWrite(ledPin, LOW);
void loop() {
int pitch = map (sensorValue, sensorLow, sensorHigh, 50, 4000);

tone(8, pitch, 20);


Apart from the lack of code tags?
The fact that you needlessly map the same value (hint) over and over again in loop()?
Too many globals?

What do you mean?

What is it not doing that you expect it to?

Is it your intention that it maintains the same tone each time its ran?

btw this is the error message :

sketch_jun10a.ino: In function 'void setup()':
sketch_jun10a.ino:24:13: error: a function-definition is not allowed here before '{' token
sketch_jun10a.ino:31:1: error: expected '}' at end of input

You didn't close "setup()"

Thanks AWOL

You need a } to close the setup loop, just before void loop() – Nevermind, you got that already.

Also, while(millis()<5000) will not work properly.

You need to assign millis() to a variable when you want to start the timer, then compare the current millis() to that variable:

startTime = millis();

Otherwise your code will only run for 5 seconds every 70 days, if I remember correctly.

Also, while(millis()<5000) will not work properly

Yes, it will.

Unless I'm misunderstanding and you want that code to run for 5 seconds after startup.

Unless I'm misunderstanding and you want that code to run for 5 seconds after startup.

You got it.

Another thing, you might want to move the while loop in you setup to the loop() function. As it is now, A0 will only be checked for 5 seconds during setup, and the last values for sensorLow, sensorHigh and sensorValue will be used in the loop() function forever, they will never change.

millis() returns the milliseconds passed since the program has started. This value will increment, and therefore it is completely valid :slight_smile: