Isn't 'serial' supposed to be understood?

This stupid script worked days ago.

This is a solar tracker script from the Arduino site with dozens of users. I’m sure it works. I removed all references to motors, s it’s only using LDR to detect light and report it in serial monitor. That part of the program was working.

I need to understand why I get this error:
Compiling .pio\build\esp32dev\FrameworkArduino\MD5Builder.cpp.o
src\main.cpp:32:2:

error: ‘Serial’ does not name a type

Serial.print(eastLDR);

//Sun Tracker Sketch
//
//This sketch is designed for use with a 9gram servo, able
//to be powered directly from the Arduinio without an external
//power source. For fritzing diagram, see Github repository
//https://github.com/nickalanf/Arduino–Projects
//The Serial monitor section is for debugging purposes, or for general interest
//one once the device is functioning correctly, can be diasabled
//
//Sketch by FIELDING - 8/2/18
#include <Arduino.h>
int eLDRPin = 2; // Assign pins to the LDR’s
int wLDRPin = 4;
int eastLDR = 0; //Create variables to store to LDR readings
int westLDR = 0;
int difference = 0; //Create a variable to compare the two LDR’s
int error = 10; // Variable for is there is a noticable difference between the two LDR’s
void setup() {
}
void loop() {
Serial.begin(9600);
eastLDR = analogRead(eLDRPin); //Read the LDR values
westLDR = analogRead(wLDRPin);
difference = eastLDR - westLDR ; //Check the difference
}
Serial.print(eastLDR); //Serial monitor can be useful for debugging/setting up
Serial.print(" - “); //Use it to see if your LDR’s are noticeably different when
Serial.print(westLDR); //They have equal light shining on them, if so, correct with the error value
Serial.print(” - “);
Serial.print(difference);
Serial.print(” - “);
Serial.print(” - “);
Serial.println(”.");
delay(1000);
}

CONFIGURATION: Redirecting...
PLATFORM: Espressif 32 1.12.4 > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:

  • framework-arduinoespressif32 3.10004.200129 (1.0.4)
  • tool-esptoolpy 1.20600.0 (2.6.0)
  • toolchain-xtensa32 2.50200.80 (5.2.0)
    LDF: Library Dependency Finder → Library Dependency Finder (LDF) — PlatformIO latest documentation
    LDF Modes: Finder ~ chain, Compatibility ~ soft
    Found 34 compatible libraries
    Scanning dependencies…
    No dependencies
    Building in release mode
    Compiling .pio\build\esp32dev\src\main.cpp.o
    Compiling .pio\build\esp32dev\FrameworkArduino\Print.cpp.o
    Compiling .pio\build\esp32dev\FrameworkArduino\Stream.cpp.o
    Compiling .pio\build\esp32dev\FrameworkArduino\StreamString.cpp.o
    src\main.cpp:32:2: error: ‘Serial’ does not name a type
    Serial.print(eastLDR); //Serial monitor can be useful for debugging/setting up
    ^
    src\main.cpp:33:2: error: ‘Serial’ does not name a type
    Serial.print(" - “); //Use it to see if your LDR’s are noticeably different when
    ^
    src\main.cpp:34:2: error: ‘Serial’ does not name a type
    Serial.print(westLDR); //They have equal light shining on them, if so, correct with the error value
    ^
    src\main.cpp:35:2: error: ‘Serial’ does not name a type
    Serial.print(” - “);
    ^
    src\main.cpp:36:2: error: ‘Serial’ does not name a type
    Serial.print(difference);
    ^
    src\main.cpp:37:2: error: ‘Serial’ does not name a type
    Serial.print(” - “);
    ^
    src\main.cpp:38:2: error: ‘Serial’ does not name a type
    Serial.print(” - “);
    ^
    src\main.cpp:39:2: error: ‘Serial’ does not name a type
    Serial.println(”.");
    ^
    src\main.cpp:40:7: error: expected constructor, destructor, or type conversion before ‘(’ token
    delay(1000);
    ^
    src\main.cpp:41:2: error: expected declaration before ‘}’ token
    }
    ^
    *** [.pio\build\esp32dev\src\main.cpp.o] Error 1
    ================================================= [FAILED]

I think your code is “broken” :frowning_face: This, from your first post with code:

void setup() {
}
void loop() {
Serial.begin(9600);
eastLDR = analogRead(eLDRPin); //Read the LDR values
westLDR = analogRead(wLDRPin);
difference = eastLDR - westLDR ; //Check the difference
}
Serial.print(eastLDR); //Serial monitor can be useful for debugging/setting up
Serial.print(" - “); //Use it to see if your LDR’s are noticeably different when
Serial.print(westLDR); //They have equal light shining on them, if so, correct with the error value
Serial.print(” - “);
Serial.print(difference);
Serial.print(” - “);
Serial.print(” - “);
Serial.println(”.");
delay(1000);
}

Should be, I think, this:

void setup() {
  // Add this...
  Serial.begin(9600);
}

void loop() {
  // Remove this line...
  //Serial.begin(9600);

eastLDR = analogRead(eLDRPin); //Read the LDR values
westLDR = analogRead(wLDRPin);
difference = eastLDR - westLDR ; //Check the difference


// Remove this brace..
// }

// Now the remainder is once more part of loop().
Serial.print(eastLDR); //Serial monitor can be useful for debugging/setting up
Serial.print(" - “); //Use it to see if your LDR’s are noticeably different when
Serial.print(westLDR); //They have equal light shining on them, if so, correct with the error value
Serial.print(” - “);
Serial.print(difference);
Serial.print(” - “);
Serial.print(” - “);
Serial.println(”.");
delay(1000);
}

HTH

Cheers,
Norm.

1 Like