problem with uploading code to nano

Hi Folks,

I'm a newbie to coding and I keep getting this error when trying to upload. It's probably something small
but I can't seem to get the code uploaded to the board.

Any help would be great, thanks

Here's is the code:

/*

HEATPAD CONTROL

Turns on and off a LED connected to digital pin 7,
and a heatpad connected to digital pin 3
when triggerd by a sensor connected to pin A0.

*/

const int outPin = 7; // select output pin (LED)
const int heatPin = 3; // select output pin (heatpad)
int sensorPin = A0; // select input pin (sensor)
int lightLevel, high = 0, low = 1023;

void setup() {
pinMode(outPin, OUTPUT);
pinMode(heatPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the value from the sensor:
int sensor_value = analogRead(0);
int lightLevel = analogRead(0);
int manualTune();
analogWrite(300, 800);
if (sensor_value < 15)
// prints the value into the serial monitor
Serial.print(sensor_value );
// turn the LED on - this is just to show circuit is working
digitalWrite(outPin, HIGH);
// print to serial monitor
Serial.println("-Light on, yurt");
// turn the heatpad on
digitalWrite(heatPin, HIGH);
// print to serial monitor
Serial.println("-heat on yurt");
delay(1000);
}

void manualTune() {
lightLevel = map(lightLevel, 0, 1023, 0, 255);
lightLevel = constrain(lightLevel, 0, 255);
}

else {
// prints the value into the serial monitor
Serial.print(sensor_value);
// turn the LED off
digitalWrite(outPin, LOW);
// prints the value into the serial monitor
Serial.println("-Light off");
// turn the heat pin off
digitalWrite(heatPin, LOW);
// prints the value into the serial monitor
Serial.println("-heat off");
// delay for ease of reading
delay(1000);
}

}

Please read the "how to use this forum-please read" stickies to see how to format and post code and error messages. It is always a good idea to include the entire content of error messages in your post.

See the ***** comments for what I did to fix the code so that it will compile.

/*
  HEATPAD CONTROL

  Turns on and off a LED connected to digital pin 7,
  and a heatpad connected to digital pin 3
  when triggerd by a sensor connected to pin A0.
*/

const int outPin = 7;             // select output pin (LED)
const int heatPin = 3;            // select output pin (heatpad)
int sensorPin = A0;         // select input pin (sensor)
int lightLevel, high = 0, low = 1023;

void setup()
{
  pinMode(outPin, OUTPUT);
  pinMode(heatPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  // read the value from the sensor:
  int sensor_value = analogRead(0);
  int lightLevel = analogRead(0);
  int manualTune();
  analogWrite(300, 800);  // ****** do you really have a pin 300?
  if (sensor_value < 15)

  { // ******  added bracket to enclose if block

    // prints the value into the serial monitor
    Serial.print(sensor_value );
    // turn the LED on - this is just to show circuit is working
    digitalWrite(outPin, HIGH);
    // print to serial monitor
    Serial.println("-Light on, yurt");
    // turn the heatpad on
    digitalWrite(heatPin, HIGH);
    // print to serial monitor
    Serial.println("-heat on yurt");
    delay(1000);

  } // ********  added bracket to close if block
    //  ******** moved  manualTune function definition out of loop()

  else 
  {
    // prints the value into the serial monitor
    Serial.print(sensor_value);
    // turn the LED off
    digitalWrite(outPin, LOW);
    // prints the value into the serial monitor
    Serial.println("-Light off");
    // turn the heat pin off
    digitalWrite(heatPin, LOW);
    // prints the value into the serial monitor
    Serial.println("-heat off");
    // delay for ease of reading
    delay(1000);
  }
}

void manualTune() 
{
  lightLevel = map(lightLevel, 0, 1023, 0, 255);
  lightLevel = constrain(lightLevel, 0, 255);
}

Thanks groundFungus, that has helped a a lot, the code has uploaded to the board but I'm having an issue with the photoresistor, it isn't receptive. When I cover it nothing at all is happening. I've played around with the code changed values and still nothing, is there something I'm doing wrong?

This is the altered code:

/*
HEATPAD CONTROL

Turns on and off a LED connected to digital pin 7,
and a heatpad connected to digital pin 3
when triggerd by a sensor connected to pin A0.
*/

const int outPin = 7; // select output pin (LED)
const int heatPin = 3; // select output pin (heatpad)
int sensorPin = A0; // select input pin (sensor)
int lightLevel, high = 0, low = 1023;

void setup()
{
pinMode(outPin, OUTPUT);
pinMode(heatPin, OUTPUT);
Serial.begin(9600);
}

void loop()
{
// read the value from the sensor:
int sensor_value = analogRead(0);
int lightLevel = analogRead(0);
int manualTune();
analogWrite(7,1023);

if (sensor_value < 15)

{

// prints the value into the serial monitor
Serial.print(sensor_value );
// turn the LED on - this is just to show circuit is working
digitalWrite(outPin, HIGH);
// print to serial monitor
Serial.println("-Light on, yurt");
// turn the heatpad on
digitalWrite(heatPin, HIGH);
// print to serial monitor
Serial.println("-heat on yurt");
delay(1000);

}

else
{
// prints the value into the serial monitor
Serial.print(sensor_value);
// turn the LED off
digitalWrite(outPin, LOW);
// prints the value into the serial monitor
Serial.println("-Light off");
// turn the heat pin off
digitalWrite(heatPin, LOW);
// prints the value into the serial monitor
Serial.println("-heat off");
// delay for ease of reading
delay(1000);
}
}

void manualTune()
{
lightLevel = map(lightLevel, 300, 800, 0, 255);
lightLevel = constrain(lightLevel, 0, 255);
}

 int lightLevel = analogRead(0);

This line adds a local variable that has the same name as the global variable. The global variable lightLevel (value = 0) is used in the manualTune() function so .... Remove the int so that the analogRead goes to the global version (there will be only the global version).
Also remove the int from in front of the call to the manualTune() function.
Like so:

lightLevel = analogRead(0);
manualTune();

See here for a tutorial on variable scope (explains what local and global scope means).

Please read the "how to use this forum-please read" stickies to see how to format and post code. Posting in code tags makes it much easier to copy to a text editor for examination.

Thanks groundFungus, appreciate that. I've made the alterations but still nothing happening to the LED, the code is uploading fine but the LED isin't doing what I'm asking it. The LED is lighting alright but the photoresistor is not co operating. Sorry to be a pain.

Here's is the current stiuation with the code:

/*
HEATPAD CONTROL

Turns on and off a LED connected to digital pin 7,
and a heatpad connected to digital pin 3
when triggerd by a sensor connected to pin A0.
*/

const int outPin = 7; // select output pin (LED)
const int heatPin = 3; // select output pin (heatpad)
int sensorPin = A0; // select input pin (sensor)
int lightLevel, high = 0, low = 1023;

void setup()
{
pinMode(outPin, OUTPUT);
pinMode(heatPin, OUTPUT);
Serial.begin(9600);
}

void loop()
{
// read the value from the sensor:
int sensor_value = analogRead(0);
lightLevel = analogRead(0);
manualTune();
analogWrite(7, 1023);

if (sensor_value < 15)

{

// prints the value into the serial monitor
Serial.print(sensor_value );
// turn the LED on - this is just to show circuit is working
digitalWrite(outPin, HIGH);
// print to serial monitor
Serial.println("-Light on, yurt");
// turn the heatpad on
digitalWrite(heatPin, HIGH);
// print to serial monitor
Serial.println("-heat on yurt");
delay(1000);

}

else
{
// prints the value into the serial monitor
Serial.print(sensor_value);
// turn the LED off
digitalWrite(outPin, LOW);
// prints the value into the serial monitor
Serial.println("-Light off");
// turn the heat pin off
digitalWrite(heatPin, LOW);
// prints the value into the serial monitor
Serial.println("-heat off");
// delay for ease of reading
delay(1000);
}
}

void manualTune()
{
lightLevel = map(lightLevel, 0, 1023, 0, 255);
lightLevel = constrain(lightLevel, 0, 255);
}

What sensor_values are printed to serial monitor while the sketch is running? Can you post a schematic of your witing, please? And your latest code?

LED isin't doing what I'm asking it.

What is the LED supposed to do? Fade, flash,...?

The LED is supposed to just turn on and off. Basically the circuit is fine. I'm heating a heat pad but I want to trigger it with the photoresistor if you get me. Essentially the LED is always on. The heat pad is heating but when I cover the photoresistor nothing is happening to the LED or the heated.

Essentially it's always running. I want to be able to trigger the circuit with the photoresists

The values I'm getting in the serial monitor are as follows,

-heat on yurt
0-Light on, yurt
-heat on yurt
0-Light on, yurt
-heat on yurt
0-Light on, yurt

The latest code is as follows:

/*
HEATPAD CONTROL

Turns on and off a LED connected to digital pin 7,
and a heatpad connected to digital pin 3
when triggerd by a sensor connected to pin A0.
*/

const int outPin = 7; // select output pin (LED)
const int heatPin = 3; // select output pin (heatpad)
int sensorPin = A0; // select input pin (sensor)
int lightLevel, high = 0, low = 1023;

void setup()
{
pinMode(outPin, OUTPUT);
pinMode(heatPin, OUTPUT);
Serial.begin(9600);
}

void loop()
{
// read the value from the sensor:
int sensor_value = analogRead(0);
lightLevel = analogRead(0);
manualTune();
analogWrite(7, 1023);

if (sensor_value < 15)

{

// prints the value into the serial monitor
Serial.print(sensor_value );
// turn the LED on - this is just to show circuit is working
digitalWrite(outPin, HIGH);
// print to serial monitor
Serial.println("-Light on, yurt");
// turn the heatpad on
digitalWrite(heatPin, HIGH);
// print to serial monitor
Serial.println("-heat on yurt");
delay(1000);

}

else
{
// prints the value into the serial monitor
Serial.print(sensor_value);
// turn the LED off
digitalWrite(outPin, LOW);
// prints the value into the serial monitor
Serial.println("-Light off");
// turn the heat pin off
digitalWrite(heatPin, LOW);
// prints the value into the serial monitor
Serial.println("-heat off");
// delay for ease of reading
delay(1000);
}
}

void manualTune()
{
lightLevel = map(lightLevel, 0, 1023, 0, 255);
lightLevel = constrain(lightLevel, 0, 255);
}

Here's an image of the circuit. The circuit is fine as I've tested it with the pad and the pad is heating perfectly, I think it's to do with what I'm telling the nano to do via the code.

0-Light on, yurt
-heat on yurt
0-Light on, yurt
-heat on yurt
0-Light on, yurt

The sensor values are all zero. That shouldn't happen if the photoresistor is wired right.

I notice that the top power rails do not have the jumpers to connect the two sides.

groundFungus, you're a legend. I should have spotted that. Apologies and thanks a million for your time. I appreciate it. It is working fine now.

Some notes on your code that may help/make the code easier to read.

  1. You declare constants to define pin names:-
const int outPin = 7;             // select output pin (LED)
const int heatPin = 3;            // select output pin (heatpad)
int sensorPin = A0;         // select input pin (sensor)

But for some reason you define the analogue input pin as an integer variable. You might as well make it a constant and maintain consistency.

Having defined names you might as well use them rather than the pin numbers e.g.

  int sensor_value = analogRead(0);
  lightLevel = analogRead(0);

Would be easier to read as

  int sensor_value = analogRead(sensorPin);
  lightLevel = analogRead(sensorPin);

You might want to rename pin 7 to lightPin or ledPin to make it more meaningful than outPin.

  1. You have defined two variables you never use in this line:-
int lightLevel, high = 0, low = 1023;

Which defines three integer variables, lightLevel, high (preset to 0) and low (preset to 1023). You never use "high" and "low" anywhere in your code.

  1. You misuse analogWrite() in this line:-
analogWrite(7, 1023);

First of all pin 7 on a Nano does not support analogueWrite() only pins 3, 5, 6, 9, 10 and 11 do. Secondly the range of values you can write to an analogue out pin is 0-255, see the info page on analogWrite() here.

If you want pin 7 set high use this code instead:-

digitalWrite(outPin, HIGH);
  1. You have defined a function manualTune():-
void manualTune()
{
  lightLevel = map(lightLevel, 0, 1023, 0, 255);
  lightLevel = constrain(lightLevel, 0, 255);
}

The second line is redundant as the input is a value from an analogRead() call which will always return a value between 0 and 1023 hence the output from the map function will always be between 0 and 255. See the info pages on analogRead() here and map() here.

As you only use the manualTune() function once you could replace this code:-

  int sensor_value = analogRead(0);
  lightLevel = analogRead(0);
  manualTune();

with this:-

  int sensor_value = analogRead(sensorPin);
  lightLevel = map(sensor_value, 0, 1023, 0, 255);

Hope this helps, it should make your code easier to read and easier to follow if you come back to it in a few months time.

Ian