Strange Arduino error messsage

The Attached sketch was cut and pasted from a site.

I saved to my computer as a Sketch and all works fine,sometimes.

The receiver sketch is ok but the transmitter sketch sometimes compiles but more than not,

it wont compile and throws up the error;

“a function-definition is not allowed here before ‘{’ token”.

I can try to compile the sketch repeatedly but 8 out of 10 times it wont compile.

Can anyone help me diagnose the problem.

Have tried it on several different UNO’s

// TRANSMITTER

void setup() {
  #include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
#include <String.h>

RH_ASK driver;

float volt_sensor, current_sensor;
int trip_sensor;

void setup()
{
    Serial.begin(9600);   
    if (!driver.init())
    Serial.println("init failed");
   
}

void loop()
{

 volt_sensor = random(200, 245); // generate some random Data
 current_sensor = random(0, 500);
 trip_sensor =random(1, 4);

    String final_data = String(volt_sensor) + "|" + String(current_sensor) + "|" + String(trip_sensor); //final_data hold all varibles.
    const char *msg = final_data.c_str();;
    driver.send((uint8_t *)msg, strlen(msg));
    driver.waitPacketSent();
    delay(1000);
}

Error Message.txt (1.86 KB)

The code you posted there goes like this:

void setup() {
 
void setup()
{

You are starting a new setup() inside the original. Apart from the fact you can’t have 2x setup()s anyway, even if you could, you can’t define any function inside another one.

but 8 out of 10 times it wont compile.

So that code is surely not compiling even 2x out of 10…

Thank you.

Its so obvious when its pointed out to you.

I think it may have compiled the few odd times I used the original sketch

It seems that I didn't clear out the new sketch before pasting in the new text.

Appreciate your help

Farticus:
Appreciate your help

I am not without sin, I cast no stones :wink: