Error during compiling - "exit status 1".

I am new to Arduino and C++.

I am building a pump speed controller to cool my CO2 CNC laser - via controlling the amount of fluid pumped through a large thermal mass (300’ of pex embedded in the concrete floor of my workshop). The arduino UNO will use two DS18B20 temp sensors, a hal flow sensor and a small OLED to display its status.

I have put together a sketch based on similar projects (credit given at beginning of code). I have tested my hardware and wiring configuration one component at a time - it all works fine.

But I get an error when trying to compile the code:

exit status 1
a function-definition is not allowed here before '{' token

I am not a code writer by any means, I am much better with hardware. I am sure I just made a simple error (or several) but cannot it/them.

Code:

/*
 Thanks to curnow on/at http://forum.arduino.cc for compiling the original code.
 Thanks to http://www.hobbytronics.co.uk for help with differential temp and onewire setup. 
 Thanks to the gang at http://forum.arduino.cc (Special thanks to PaulS)for help with PWM 
 control issues.
 Thanks to http://arduino-er.blogspot.com
 Thanks to http://www.bc-robotics.com
 Also Jeremy Blum's Tutorials http://www.jeremyblum.com
*/


//I2C 128x64 display
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

//temp sensors
#include <OneWire.h>
#include <DallasTemperature.h>

//128x64 display
#define SDA_PIN 4
#define SCL_PIN 5
//CORRECT Adafruit_ssd1306syp display(SDA_PIN,SCL_PIN);
#define OLED_RESET LED_BUILTIN  //4
Adafruit_SSD1306 display(OLED_RESET);

byte degree[8] = {  //Degree character
  B00110,
  B01001,
  B01001,
  B00110,
  B00000,
  B00000,
  B00000,
};

// Data wire is plugged into pin 10 on the Arduino
#define ONE_WIRE_BUS 10

// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

const int motorPin = 9;

int i = 65;

//FLOW SENSOR
int flowPin = 2;    //This is the input pin on the Arduino
double flowRate;    //This is the value we intend to calculate. 
volatile int count; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.  

void setup(void)

{
  
  //128x64 display
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  // Clear the buffer.
  display.clearDisplay();
  display.display();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("HELLO SIR, LASER COOLING SYSTEM IS INITIALIZING:");
  display.display();

  //pump control
  pinMode(motorPin, OUTPUT);

  Serial.begin(9600);  // start serial port

  sensors.begin();  // Start up the library

  //FLOW SENSOR
  pinMode(flowPin, INPUT);           //Sets the pin as an input
  //CORRECT attachInterrupt(0, Flow, RISING);  //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"  
}

void loop()
{
  //FLOW SENSOR
  count = 0;      // Reset the counter so we start counting from 0 again
  interrupts();   //Enables interrupts on the Arduino
  delay (1000);   //Wait 1 second 
  noInterrupts(); //Disable the interrupts on the Arduino
  
  //Start the math
  flowRate = (count * 2.25);        //Take counted pulses in the last second and multiply by 2.25mL 
  flowRate = flowRate * 60;         //Convert seconds to minutes, giving you mL / Minute
  flowRate = flowRate / 1000;       //Convert mL to Liters, giving you Liters / Minute

  Serial.println(flowRate);         //Print the variable flowRate to Serial

  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  sensors.requestTemperatures(); // Send the command to get temperatures

  int CoolantTemp = sensors.getTempCByIndex(0); 
  int LaserTemp = sensors.getTempCByIndex(1);
  int CompareTemps = (LaserTemp - CoolantTemp);

  int val = i; //Store last motorPin value

  int pct = map(i,0,255,0,100); //Calculate PWM percentage

  Serial.print("CoolentTemp ");
  Serial.print(CoolantTemp); 
  
  Serial.print(" LaserTemp ");
  Serial.print(LaserTemp);

  if (LaserTemp  <= 20) /*If laser temp 20Deg or lower turn pump minimum speed.*/
  {
    if(i > 50)
    {
      i = 0;
    }
    analogWrite (motorPin, i); 
    Serial.print (" MINIMUM ");
  }

  else if  (LaserTemp > 22) /* If laser greater then 22Deg 
                                   ...increase motor speed by 10
                               */   
  {
    if(i <= 245)
    { 
      i = i + 10; 
    } 

    analogWrite (motorPin, i);            
    Serial.print("  Pump Increasing ");
  }

  else if ((LaserTemp > 20) && (LaserTemp <= 21) && (i >75)) /*If LaserTemp between 20-21 deg, don't increase
                                                                     or decrease speed.
                                                                    */
  {
    if (i <= 255)
    {
      i = val;
    }  


    analogWrite (motorPin, i);            
    Serial.print("  Pump Stable ");
  }


  else if ((LaserTemp <= 19) && (i >75)) /*If laser temp less than 19
                                                                     motor speed by 10
                                                                    */
  { 

    {
      i = i - 10; 
    }

    analogWrite (motorPin, i);            
    Serial.print("  Pump Decreasing ");
  }

  else

  {
    {
      i = 50;
      //50 = pump minimum speed
    }

    analogWrite (motorPin, i);
    Serial.print("  Pump Minumim ");
    //CORRECT int Status = MINIMUM
}
  
{
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  // Clear the buffer.
  display.clearDisplay();
  display.display();

  display.setTextSize(1);
  display.setTextColor(WHITE);


  display.setCursor(0,0);
  display.println("LASER TEMP:");
  display.println(LaserTemp);
  display.println(" C");

  display.setCursor(0,1);
  display.println("COOLANT TEMP:");
  display.println(CoolantTemp);
  display.println(" C");

  display.setCursor(0,2);
  display.println("COOLANT FLOW:");
  display.println(flowRate);
  display.println(" LPM");

  display.setCursor(0,3);
  display.println("PUMP OPERATING:");
  display.println(pct);
  display.println("%");
}
{
  Serial.print(pct); // Print PWM percentage (Thanks wildbill)
  Serial.println("%");
  Serial.println();
  delay (1000);
}

void Flow()
{
count++; //Every time this function is called, increment "count" by 1
}

I did some research trying to figure this out but I don’t think I have the proper knowledge base to build upon and solve the problem.

Any pointers would be greatly appreciated. Thanks, Travis

Try to match your { with your }.

Looks to me like Flow is incorrect.

Even without all the extraneous blank lines and useless, and mis-matched, curly braces, that code is crap.

You do nothing with interrupts enabled except stick your head in the sand. Then, for the rest of loop(), interrupts are disabled.

Ditch ALL of the code from the interrupts() line to the noInterrupts() line. Then, look at the blink without delay example to see how to do things on a regular basis, like compute flow rate, without using delay().

Thanks Grove, You pointed me in the right direction - Flow was correct, but the corresponding code was not.

PaulS, I looked over the blinkwithoutdelay example and I can see why the method would be better but I don’t think I have the skill to implement it - but maybe more importantly, the time to try right now, my CNC laser is for my business and it is down until I get this controller online - old cooler failed.

I was able to find all of the redundant braces. I did not clean up the spaces, I will later after I am done fine tuning the pump and a larger display when it arrives. I have bad vision (not from the laser:), so the extra lines make it easier to read the code.

It is working now, I still need to fine tune the pump but otherwise good.

I am already in love with the arduino - I have many more ideas and will likely be spending more time on here when I have a chance.

Thank you both, Travis

Corrected code for anyone who might care:

/*
 Thanks to curnow on/at http://forum.arduino.cc for compiling the original code.
 Thanks to http://www.hobbytronics.co.uk for help with differential temp and onewire setup. 
 Thanks to the gang at http://forum.arduino.cc (Special thanks to PaulS)for help with PWM 
 control issues.
 Thanks to http://arduino-er.blogspot.com
 Thanks to http://www.bc-robotics.com
 Also Jeremy Blum's Tutorials http://www.jeremyblum.com
*/


//I2C 128x64 display
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

//temp sensors
#include <OneWire.h>
#include <DallasTemperature.h>

//128x64 display
#define SDA_PIN 4
#define SCL_PIN 5
//CORRECT Adafruit_ssd1306syp display(SDA_PIN,SCL_PIN);
#define OLED_RESET LED_BUILTIN  //4
Adafruit_SSD1306 display(OLED_RESET);

byte degree[8] = {  //Degree character
  B00110,
  B01001,
  B01001,
  B00110,
  B00000,
  B00000,
  B00000,
};

// Data wire is plugged into pin 10 on the Arduino
#define ONE_WIRE_BUS 10

// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

const int motorPin = 9;

int i = 65;

//FLOW SENSOR
int flowPin = 2;    //This is the input pin on the Arduino
double flowRate;    //This is the value we intend to calculate. 
volatile int count; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.  

void setup(void)

{
  
  //128x64 display
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  // Clear the buffer.
  display.clearDisplay();
  display.display();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("HELLO SIR, LASER COOLING SYSTEM IS INITIALIZING:");
  display.display();

  //pump control
  pinMode(motorPin, OUTPUT);

  sensors.begin();  // Start up the library

  //FLOW SENSOR
  pinMode(flowPin, INPUT);           //Sets the pin as an input
  attachInterrupt(0, Flow, RISING);  //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"  
  Serial.begin(9600);  // start serial port
}

void loop()
{
  //FLOW SENSOR
  count = 0;      // Reset the counter so we start counting from 0 again
  interrupts();   //Enables interrupts on the Arduino
  delay (1000);   //Wait 1 second 
  noInterrupts(); //Disable the interrupts on the Arduino
  
  //Start the math
  flowRate = (count * 2.25);        //Take counted pulses in the last second and multiply by 2.25mL 
  flowRate = flowRate * 60;         //Convert seconds to minutes, giving you mL / Minute
  flowRate = flowRate / 1000;       //Convert mL to Liters, giving you Liters / Minute

  Serial.print(flowRate);         //Print the variable flowRate to Serial
  Serial.print("LPM ");

  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  sensors.requestTemperatures(); // Send the command to get temperatures

  int CoolantTemp = sensors.getTempCByIndex(0); 
  int LaserTemp = sensors.getTempCByIndex(1);
  int CompareTemps = (LaserTemp - CoolantTemp);

  int val = i; //Store last motorPin value

  int pct = map(i,0,255,0,100); //Calculate PWM percentage

  Serial.print("CoolentTemp ");
  Serial.print(CoolantTemp); 
  
  Serial.print(" LaserTemp ");
  Serial.print(LaserTemp);

  if (LaserTemp  <= 20) /*If laser temp 20Deg or lower turn pump minimum speed.*/
  {
    if(i > 50)
    {
      i = 126; //WAS 0
    }
    analogWrite (motorPin, i); 
    Serial.print (" MINIMUM ");

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(60,24);
  display.println("MINIMUM");
  display.display();
  }

  else if  (LaserTemp > 22) /* If laser greater then 22Deg 
                                   ...increase motor speed by 10
                               */   
  {
    if(i <= 248)
    { 
      i = i + 10; 
    } 

    analogWrite (motorPin, i);            
    Serial.print("  Pump Increasing ");

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(65,24);
  display.println("INCREASING");
  display.display();
  }

  else if ((LaserTemp > 20) && (LaserTemp <= 21) && (i >75)) /*If LaserTemp between 20-21 deg, don't increase
                                                                     or decrease speed.
                                                                    */
  {
    if (i <= 255)
    {
      i = val;
    }  


    analogWrite (motorPin, i);            
    Serial.print("  Pump Stable ");

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(65,24);
  display.println("STABLE");
  display.display();
  }


  else if ((LaserTemp <= 19) && (i >125)) /*If laser temp less than 19
                                                                     motor speed by 10
                                                                    */
  { 

    {
      i = i - 10; 
    }

    analogWrite (motorPin, i);            
    Serial.print("  Pump Decreasing ");

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(65,24);
  display.println("DECREASING");
  display.display();
  }

  else

  {
    {
      i = 126;
      //126 = pump minimum speed
    }

    analogWrite (motorPin, i);
    Serial.print("  Pump Minimum ");

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(65,24);
  display.println("MINIMUM");
  display.display();
}

display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  display.display();
  display.setTextSize(1);
  display.setTextColor(WHITE);


  display.setCursor(0,0);
  display.println("LASER TEMP:");
  display.setCursor(87,0);
  display.println(LaserTemp);
  display.setCursor(106,0);
  display.println(" C");

  display.setCursor(0,8);
  display.println("COOLANT TEMP:");
  display.setCursor(87,8);
  display.println(CoolantTemp);
  display.setCursor(106,8);
  display.println(" C");

  display.setCursor(0,16);
  display.println("COOLANT FLOW:");
  display.setCursor(87,16);
  display.println(flowRate);
  display.setCursor(106,16);
  display.println(" LM");

  display.setCursor(0,24);
  display.println("PUMP:");
  display.setCursor(30,24);
  display.println(pct);
  display.setCursor(50,24);
  display.println("%");
  display.display();

  // Clear the buffer.
  display.clearDisplay();
  
  Serial.print(pct); // Print PWM percentage (Thanks wildbill)
  Serial.println("%");
  Serial.println();
  delay (1000);
}

void Flow()
{
count++; //Every time this function is called, increment "count" by 1
}

When you get your equipment back on line, you REALLY need to rewrite the code completely.

unsigned long lastTime = 0;
int interval = 1000;

void loop()
{
   unsigned long thisTime = millis();
   if(thisTime - lastTime > interval)
   {
      // disable interrupts
      // copy data
      // enable interrupts

      // use the data to compute flow

      lastTime = thisTime;
   }

   // Do other stuff
}

Thank you Paul.

I will, I am now mostly caught up on work and will do this the next time I have the laser down for maintenance. Will let you know how it works out and post the revised code.

T