# Arduino nano sketch wont validate/wont upload.

#### gene-stevo

Joined Feb 22, 2019
13
Hi Folks, I'm trying to use a sketch (not my work) to control a high speed flash however when I try to validate it I get the error message: 'reading' was not declared in this scope. A certain amount of code is commented out as the sketch is a dual purpose solution for either controlling as laser trigger or a piezo vibration trigger. here is the complete sketch...There is also a screen shot of the error as shown. Thanks for reading, Cheers, Eugene.

C++:
/* This program is used as a laser trigger and/or sound trigger for high-speed photography<br>  ---> Made by Sander Vanhee <--

push button to activate program
Activate laser or sound trigger
delay
open camera shutter (manual for now)
drop an object
laser barrier or piezo sensor gets triggered
laser off
wait the delay set by potentiometer
trigger flash
delay
close camera shutter
delay
*/

int laserPin = 12;        // red wire from laser
int potPin = 0;           // potentiometer to read delay
int optoPin = 8;          // towards the optocoupler
int buttonApin = 2;       // pushbutton
int photocellPin = A1;    // the cell and 10K pulldown are connected to A1
int PiezoPin = A2;         // the Piezo is connected to a2

void setup(){
// We'll send debugging information via the Serial monitor
Serial.begin(9600);

//#define photocellPin 1
pinMode(laserPin, OUTPUT);
pinMode(buttonApin, INPUT);
pinMode(photocellPin, INPUT_PULLUP);
pinMode(PiezoPin, INPUT);
pinMode(optoPin, OUTPUT);
pinMode(potPin, INPUT_PULLUP);
}

void loop() {
// check if button is pressed, only continue when pressed.
Serial.println("Waiting for buttonpress...");

if (digitalRead(buttonApin) == HIGH) {  // start program if button is pressed
//start if(buttonpressed) loop
Serial.println("Button pressed");

reading = map(reading, 0, 1023, 1, 500);     // scale it to use it with the drops (delay between 1 and 500)

delay(50);

/* We'll put the laser parts in comments to isolate them from the Piezo code

// Activate laser
digitalWrite(laserPin, HIGH);
Serial.println("Laser Activated");

delay(1000); // delay before open shutter

Serial.println("Drop object");

// read the value of the photocellPin

//Check if laser is broken
Serial.println("Laser uninterrupted");

// as long as the laser is not broken, this "while" statement will loop.
// when the laser is broken, the program will continue.

// laser off
// turn laser off to not show up in the picture
// digitalWrite(laserPin, LOW);
*/

// what follows is the piezo code
Serial.println("no sound detected");
}

// Wait the amount indicated by the value of potentiometer
// give the waves time to rise back to the surface

//trigger flash - send signal from pin 8
digitalWrite(8, HIGH);
Serial.println("FLASH fired");
delay(1000);
digitalWrite(8, LOW);

//wait for flash light to die down
delay(200);

//close camera shutter

Serial.println("END");
// END OF (buttonpressed) while loop

digitalWrite(laserPin, LOW);
}}

void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:

}

#### djsfantasi

Joined Apr 11, 2010
7,752
Hi Folks, I'm trying to use a sketch (not my work) to control a high speed flash however when I try to validate it I get the error message: 'reading' was not declared in this scope. A certain amount of code is commented out as the sketch is a dual purpose solution for either controlling as laser trigger or a piezo vibration trigger. here is the complete sketch...
C++:
/* This program is used as a laser trigger and/or sound trigger for high-speed photography<br>  ---> Made by Sander Vanhee <--

push button to activate program
Activate laser or sound trigger
delay
open camera shutter (manual for now)
drop an object
laser barrier or piezo sensor gets triggered
laser off
wait the delay set by potentiometer
trigger flash
delay
close camera shutter
delay
*/

int laserPin = 12;        // red wire from laser
int potPin = 0;           // potentiometer to read delay
int optoPin = 8;          // towards the optocoupler
int buttonApin = 2;       // pushbutton
int photocellPin = A1;    // the cell and 10K pulldown are connected to A1
int PiezoPin = A2;         // the Piezo is connected to a2

void setup(){
// We'll send debugging information via the Serial monitor
Serial.begin(9600);

//#define photocellPin 1
pinMode(laserPin, OUTPUT);
pinMode(buttonApin, INPUT);
pinMode(photocellPin, INPUT_PULLUP);
pinMode(PiezoPin, INPUT);
pinMode(optoPin, OUTPUT);
pinMode(potPin, INPUT_PULLUP);
}

void loop() {
// check if button is pressed, only continue when pressed.
Serial.println("Waiting for buttonpress...");

if (digitalRead(buttonApin) == HIGH) {  // start program if button is pressed
//start if(buttonpressed) loop
Serial.println("Button pressed");

reading = map(reading, 0, 1023, 1, 500);     // scale it to use it with the drops (delay between 1 and 500)

delay(50);

/* We'll put the laser parts in comments to isolate them from the Piezo code

// Activate laser
digitalWrite(laserPin, HIGH);
Serial.println("Laser Activated");

delay(1000); // delay before open shutter

Serial.println("Drop object");

// read the value of the photocellPin

//Check if laser is broken
Serial.println("Laser uninterrupted");

// as long as the laser is not broken, this "while" statement will loop.
// when the laser is broken, the program will continue.

// laser off
// turn laser off to not show up in the picture
// digitalWrite(laserPin, LOW);
*/

// what follows is the piezo code
Serial.println("no sound detected");
}

// Wait the amount indicated by the value of potentiometer
// give the waves time to rise back to the surface

//trigger flash - send signal from pin 8
digitalWrite(8, HIGH);
Serial.println("FLASH fired");
delay(1000);
digitalWrite(8, LOW);

//wait for flash light to die down
delay(200);

//close camera shutter

Serial.println("END");
// END OF (buttonpressed) while loop

digitalWrite(laserPin, LOW);
}}

void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:

}
Whoever wrote the sketch is unfamiliar with how to code for an Arduino.

The main loop contains no code. The setup doesn’t define anything. For example, the potentiometer pin at least should be defined in setup(). Plus the laser or piezo should be defined as well.

Everything is defined in the global section, which is usually only used for global variable and library initialization.

The variable reading is initialized in one code block and then it is attempted to be used outside code block. That will give you the “‘reading’ is not declared inside this scope”.

It’s a wonder if this ever worked. You need to separate the code into global definitions, hardware/library setup and the main loop. And, you need to declare variable outside of the if, while, or for code blocks, if you need the value outside any of them.

#### gene-stevo

Joined Feb 22, 2019
13
Whoever wrote the sketch is unfamiliar with how to code for an Arduino.

The main loop contains no code. The setup doesn’t define anything. For example, the potentiometer pin at least should be defined in setup(). Plus the laser or piezo should be defined as well.

Everything is defined in the global section, which is usually only used for global variable and library initialization.

The variable reading is initialized in one code block and then it is attempted to be used outside code block. That will give you the “‘reading’ is not declared inside this scope”.

It’s a wonder if this ever worked. You need to separate the code into global definitions, hardware/library setup and the main loop. And, you need to declare variable outside of the if, while, or for code blocks, if you need the value outside any of them.
Thanks so much for your prompt response. Though I admit to not understanding much of it. The odd thing is the original version didn't mention the piezo stuff at all, just the laser stuff. I've build a rig using just the laser trigger stuff and it works perfectly. Once again thank you for your timely help. Cheers,
Eugene.

#### dl324

Joined Mar 30, 2015
12,917
'reading' was not declared in this scope
The error message is pretty clear. The variable 'reading' wasn't declared in a way that allows it to be used.

I didn't see it declared anywhere in the code you posted. Could it be in a header file?

#### djsfantasi

Joined Apr 11, 2010
7,752
The error message is pretty clear. The variable 'reading' wasn't declared in a way that allows it to be used.

I didn't see it declared anywhere in the code you posted. Could it be in a header file?
It’s declared inside an “if” code block.
Hence, it’s only available within that block.

#### djsfantasi

Joined Apr 11, 2010
7,752

• line 43 starts the “if” code block
• line 48 defines the variable ‘reading’ as an int
• line 91 is the end of the code block; and the end of the scope of the ‘reading’ variable
• line 95 is where ‘reading’ is referenced and not defined

#### s14rs4

Joined Sep 15, 2016
73
Hi Folks, I'm trying to use a sketch (not my work) to control a high speed flash however when I try to validate it I get the error message: 'reading' was not declared in this scope. A certain amount of code is commented out as the sketch is a dual purpose solution for either controlling as laser trigger or a piezo vibration trigger. here is the complete sketch...There is also a screen shot of the error as shown. Thanks for reading, Cheers, Eugene.
View attachment 201304
C++:
/* This program is used as a laser trigger and/or sound trigger for high-speed photography<br>  ---> Made by Sander Vanhee <--

push button to activate program
Activate laser or sound trigger
delay
open camera shutter (manual for now)
drop an object
laser barrier or piezo sensor gets triggered
laser off
wait the delay set by potentiometer
trigger flash
delay
close camera shutter
delay
*/

int laserPin = 12;        // red wire from laser
int potPin = 0;           // potentiometer to read delay
int optoPin = 8;          // towards the optocoupler
int buttonApin = 2;       // pushbutton
int photocellPin = A1;    // the cell and 10K pulldown are connected to A1
int PiezoPin = A2;         // the Piezo is connected to a2

void setup(){
// We'll send debugging information via the Serial monitor
Serial.begin(9600);

//#define photocellPin 1
pinMode(laserPin, OUTPUT);
pinMode(buttonApin, INPUT);
pinMode(photocellPin, INPUT_PULLUP);
pinMode(PiezoPin, INPUT);
pinMode(optoPin, OUTPUT);
pinMode(potPin, INPUT_PULLUP);
}

void loop() {
// check if button is pressed, only continue when pressed.
Serial.println("Waiting for buttonpress...");

if (digitalRead(buttonApin) == HIGH) {  // start program if button is pressed
//start if(buttonpressed) loop
Serial.println("Button pressed");

reading = map(reading, 0, 1023, 1, 500);     // scale it to use it with the drops (delay between 1 and 500)

delay(50);

/* We'll put the laser parts in comments to isolate them from the Piezo code

// Activate laser
digitalWrite(laserPin, HIGH);
Serial.println("Laser Activated");

delay(1000); // delay before open shutter

Serial.println("Drop object");

// read the value of the photocellPin

//Check if laser is broken
Serial.println("Laser uninterrupted");

// as long as the laser is not broken, this "while" statement will loop.
// when the laser is broken, the program will continue.

// laser off
// turn laser off to not show up in the picture
// digitalWrite(laserPin, LOW);
*/

// what follows is the piezo code
Serial.println("no sound detected");
}

// Wait the amount indicated by the value of potentiometer
// give the waves time to rise back to the surface

//trigger flash - send signal from pin 8
digitalWrite(8, HIGH);
Serial.println("FLASH fired");
delay(1000);
digitalWrite(8, LOW);

//wait for flash light to die down
delay(200);

//close camera shutter

Serial.println("END");
// END OF (buttonpressed) while loop

digitalWrite(laserPin, LOW);
}}

void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:

}
Make the declaration global by inserting "int reading;" at line 25.

Remove the "int" before "reading" on line 48.

You also have an extra curly bracket on line 112 delete this.

There is also an extra "void setup()" and "void loop()" at the end of your code, delete every thing after line 113.

It should compile now, but I can't promise it will work.

I apologize if this reply doesn't display properly but I can't get a preview of my post. This is only my second posting.

Mod edit: move code out of quote - JohnInTX

Last edited by a moderator:

#### gene-stevo

Joined Feb 22, 2019
13
C++:
/* This program is used as a laser trigger and/or sound trigger for high-speed photography<br>  ---> Made by Sander Vanhee <--

push button to activate program
Activate laser or sound trigger
delay
open camera shutter (manual for now)
drop an object
laser barrier or piezo sensor gets triggered
laser off
wait the delay set by potentiometer
trigger flash
delay
close camera shutter
delay
*/

int laserPin = 12;        // red wire from laser
int potPin = 0;           // potentiometer to read delay
int optoPin = 8;          // towards the optocoupler
int buttonApin = 2;       // pushbutton
int photocellPin = A1;    // the cell and 10K pulldown are connected to A1
int PiezoPin = A2;         // the Piezo is connected to a2

void setup(){
// We'll send debugging information via the Serial monitor
Serial.begin(9600);

//#define photocellPin 1
pinMode(laserPin, OUTPUT);
pinMode(buttonApin, INPUT);
pinMode(photocellPin, INPUT_PULLUP);
pinMode(PiezoPin, INPUT);
pinMode(optoPin, OUTPUT);
pinMode(potPin, INPUT_PULLUP);
}

void loop() {
// check if button is pressed, only continue when pressed.
Serial.println("Waiting for buttonpress...");

if (digitalRead(buttonApin) == HIGH) {  // start program if button is pressed
//start if(buttonpressed) loop
Serial.println("Button pressed");

reading = map(reading, 0, 1023, 1, 500);     // scale it to use it with the drops (delay between 1 and 500)

delay(50);

/* We'll put the laser parts in comments to isolate them from the Piezo code

// Activate laser
digitalWrite(laserPin, HIGH);
Serial.println("Laser Activated");

delay(1000); // delay before open shutter

Serial.println("Drop object");

// read the value of the photocellPin

//Check if laser is broken
Serial.println("Laser uninterrupted");

// as long as the laser is not broken, this "while" statement will loop.
// when the laser is broken, the program will continue.

// laser off
// turn laser off to not show up in the picture
// digitalWrite(laserPin, LOW);
*/

// what follows is the piezo code
Serial.println("no sound detected");
}

// Wait the amount indicated by the value of potentiometer
// give the waves time to rise back to the surface

//trigger flash - send signal from pin 8
digitalWrite(8, HIGH);
Serial.println("FLASH fired");
delay(1000);
digitalWrite(8, LOW);

//wait for flash light to die down
delay(200);

//close camera shutter

Serial.println("END");
// END OF (buttonpressed) while loop

digitalWrite(laserPin, LOW);
}}

void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:

}
Make the declaration global by inserting "int reading;" at line 25.

Remove the "int" before "reading" on line 48.

You also have an extra curly bracket on line 112 delete this.

There is also an extra "void setup()" and "void loop()" at the end of your code, delete every thing after line 113.

It should compile now, but I can't promise it will work.

I apologize if this reply doesn't display properly but I can't get a preview of my post. This is only my second posting.

Mod edit: move code out of quote - JohnInTX
Thank you so much for that. The sketch now compiles fine. The problem I now have (apart from a profound lack of understanding) is the nano wont acept the upload. I've tried uploading a default sketch, like Blink without sucess. I've tried different computers, different USB cables. Nothing works, apart from the power light and the blinking "L" light.
I've checked I'm using the correct port on each computer and selected nano from the board list.
Curiously when I check for board info this is the info I get:
BN: Unknown board
VID: 1A86
PID: 7523
SN: Upload any sketch to obtain it
After trying to upload I then get this error message:
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x18
I bought a three pack of unsoldered nano boards (e-bay) and none of them will accept an upload. Is it likely I've bought a duff batch?

#### s14rs4

Joined Sep 15, 2016
73
Thank you so much for that. The sketch now compiles fine. The problem I now have (apart from a profound lack of understanding) is the nano wont acept the upload. I've tried uploading a default sketch, like Blink without sucess. I've tried different computers, different USB cables. Nothing works, apart from the power light and the blinking "L" light.
I've checked I'm using the correct port on each computer and selected nano from the board list.
Curiously when I check for board info this is the info I get:
BN: Unknown board
VID: 1A86
PID: 7523
SN: Upload any sketch to obtain it
After trying to upload I then get this error message:
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x18
I bought a three pack of unsoldered nano boards (e-bay) and none of them will accept an upload. Is it likely I've bought a duff batch?
If they are from e-bay I assume they are Chinese clones. In the Tools menu when you select processor be sure to select ATmega328P(Old Bootloader). The newer genuine Arduino nano's use a different baud rate.

#### gene-stevo

Joined Feb 22, 2019
13
If they are from e-bay I assume they are Chinese clones. In the Tools menu when you select processor be sure to select ATmega328P(Old Bootloader). The newer genuine Arduino nano's use a different baud rate.
Thanks so much for your quick responce, that did it. Upload worked first time (so I don't need a refund}. Bloody code doesn't work though. Back to the drawing board. All I'm trying to do if fire a flash (via optocoupler) in responce to a noise/vibration detected by piezo. I'll dig around in the default sketch library.
Thanks to all who responded and for your patience with an obvious newb. Very much appreciated.

#### djsfantasi

Joined Apr 11, 2010
7,752
Thanks so much for your quick responce, that did it. Upload worked first time (so I don't need a refund}. Bloody code doesn't work though. Back to the drawing board. All I'm trying to do if fire a flash (via optocoupler) in responce to a noise/vibration detected by piezo. I'll dig around in the default sketch library.
Thanks to all who responded and for your patience with an obvious newb. Very much appreciated.
If that is all you need right now, I've modified the sketch. I removed everything related to the laser, etc... Ive corrected several errors and this slim, trim sketch is all you need given the requirements you stated above...

Arduino Nano Sketch to Listen to a Piezo device and Trigger an Optocoupler:
// This program is used as a sound trigger for high-speed photography

int potPin = D4;            // potentiometer pin to read delay
// pin 0 won't work because youre using Serial.println()
int optoPin = D8;          // towards the optocoupler
int PiezoPin = A2;         // the Piezo is connected to a2
int PiezoReading = 0;         // the analog reading from the sensor divider
int PiezoDiff = 0;

void setup() {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);

pinMode(potPin, INPUT_PULLUP);
pinMode(optoPin, OUTPUT);
pinMode(PiezoPin, INPUT);
}

void loop() {

PotReading = map(PotReading, 0, 1023, 1, 500);     // scale it to use it with the drops (delay between 1 and 500)
delay(50);

// what follows is the piezo code
Serial.println("no sound detected");
//wait for Piezo to change significantly

// Wait the amount indicated by the value of potentiometer
// give the waves time to rise back to the surface

//trigger flash - send signal from pin 8
digitalWrite(8, HIGH);
Serial.println("FLASH fired");
delay(1000);  // delay 1 second
digitalWrite(8, LOW);

//wait for flash light to die down
delay(200);

}

#### gene-stevo

Joined Feb 22, 2019
13
If that is all you need right now, I've modified the sketch. I removed everything related to the laser, etc... Ive corrected several errors and this slim, trim sketch is all you need given the requirements you stated above...

Arduino Nano Sketch to Listen to a Piezo device and Trigger an Optocoupler:
// This program is used as a sound trigger for high-speed photography

int potPin = D4;            // potentiometer pin to read delay
// pin 0 won't work because youre using Serial.println()
int optoPin = D8;          // towards the optocoupler
int PiezoPin = A2;         // the Piezo is connected to a2
int PiezoReading = 0;         // the analog reading from the sensor divider
int PiezoDiff = 0;

void setup() {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);

pinMode(potPin, INPUT_PULLUP);
pinMode(optoPin, OUTPUT);
pinMode(PiezoPin, INPUT);
}

void loop() {

PotReading = map(PotReading, 0, 1023, 1, 500);     // scale it to use it with the drops (delay between 1 and 500)
delay(50);

// what follows is the piezo code
Serial.println("no sound detected");
//wait for Piezo to change significantly

// Wait the amount indicated by the value of potentiometer
// give the waves time to rise back to the surface

//trigger flash - send signal from pin 8
digitalWrite(8, HIGH);
Serial.println("FLASH fired");
delay(1000);  // delay 1 second
digitalWrite(8, LOW);

//wait for flash light to die down
delay(200);

}
Huge thanks for that. I'll have another play tomorrow. Can't thank you enough for your trouble.
Cheers,
Eugene