[Solved] Virtual Button to Control "Mode" Error Compiling

Hi all. I am working on controlling some modes (basically turn one or more select relays on, one or more select relays off) when a virtual button is pushed (V3). Once I get the command structure down I can build out everything I need however I keep hitting a wall when I follow examples I’ve find elsewhere on this community for doing something like this, namely I am getting a error while compiling:

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

This is the code, I’ve been putting in Setup, but I tried it everywhere but loop. I am using SimpleTimers.

 // Managing "Modes" For Xbox, etc.
  BLYNK_WRITE(3)
  {
    int i = param.asInt();
    if (i == 1)
    {
      digitalWrite(RELAY1, HIGH);
      digitalWrite(RELAY2, LOW);
    }
    else
    {
      digitalWrite(RELAY2, HIGH);
      digitalWrite(RELAY1, LOW);
    }
  }

Any ideas what I am doing wrong? :smile:

Please post full code. Look like problem is not in this peace.

1 Like

Sure thing:

// included library code:
#include <LiquidCrystal.h>
#include <IRremote.h>
#include <SimpleTimer.h>
#include <SPI.h>
#include <BlynkSimpleEthernet.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(43, 42, 41, 40, 39, 38);

char auth[] = "2e12807d32d441b0a24ccce76b224e08";

// Initialze IR Receive (Not functioning as of 1/7/2016)
int RECV_PIN = 34;
IRrecv irrecv(RECV_PIN);
decode_results results;

// temperature sensor goodies
const int sensorPin = A14;
const int sensorPin2 = A15;
const float baselineTemp = 18.0;

// Relays that are used to control the power to all the network devices
#define RELAY1  22 // 1 on Sainsmart Powers Outlet 1 on HTS3600                       
#define RELAY2  23 // 2 on Sainsmart Powers Outlet 2 on HTS3600                       
#define RELAY3  24 // 6 on Sainsmart Powers Outlet 3 on HTS3600                        
#define RELAY4  25 // 7 on Sainsmart Powers Outlet 4 on HTS3600 
#define RELAY5  26 // 8 on Sainsmart Powers Outlet 5 on HTS3600 

// Relay Output Pin for Cooling Fan Control to 12V Source
#define RELAY9  27 // 9 on Sainsmart Cabinet Cooling Fan
#define RELAY10 28 // 10 on Sainsmart Power Unit Cooling Fan

// There must be one global SimpleTimer object.
SimpleTimer timer;

//Blynk Serial Print
#define BLYNK_PRINT Serial



// Attach virtual serial terminal to Virtual Pin V1
WidgetTerminal terminal(V2);

void BCRead() {
  //what we want to run every 10 milli-seconds here:
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    terminal.print(results.value, HEX);
    irrecv.resume(); // Receive the next value
  }
  delay(100);

  if (irrecv.decode(&results)) {

    if (results.value == 0xE0E036C9) { // Red Button
      digitalWrite(27, LOW);
      delay(100);
    }
    if (results.value == 0xE0E028D7) { // Green Button
      digitalWrite(27, HIGH);
      delay(100);
    }
    if (results.value == 0xE0E0A857) { // Yellow Button
      digitalWrite(28, LOW);
      delay(100);
    }
    if (results.value == 0xE0E06897) { // Blue Button
      digitalWrite(28, HIGH);
      delay(100);
    }
    if (results.value == 0xE0E0629D) { // Stop Button
      Serial.println("Stop Button");
      delay(100);
    }

    irrecv.resume(); // Receive the next value

  }
  delay(100);
}
// End of BCRead timed task


void FanLCDTask() {
  //Our main temperature and LCD controls running every 10 seconds here:

  // read sensor value for 2xTMP36
  int sensorVal = analogRead(sensorPin);
  int sensorVal2 = analogRead(sensorPin2);

  // set the cursor to column 0, line 1
  lcd.setCursor(0, 0);
  //convert the ADC rating to voltage
  float voltage = (sensorVal / 1024.0) * 5;
  float voltage2 = (sensorVal2 / 1024.0) * 5;
  // convert the voltage to temperature in degrees C
  float temperatureC = (voltage - .5) * 100;
  float temperatureC2 = (voltage2 - .5) * 100;
  // convert to Farenheit
  float temperatureF = (temperatureC * 9.0 / 5) + 32.0;
  float temperatureF2 = (temperatureC2 * 9.0 / 5) + 32.0;
  //display Temperature on LCD
  lcd.print(temperatureF);
  lcd.print((char)223);
  lcd.print("F ");
  Blynk.virtualWrite(0, temperatureF);
  Blynk.virtualWrite(1, temperatureF2);
  delay(100); // (time to settle)

  // Cabinet Fan control
  if (temperatureF > 75)
  {
    digitalWrite(27, LOW);
    lcd.setCursor (8, 0);
    lcd.print ("COOLING");
  }

  else

  {
    digitalWrite(27, HIGH);
    lcd.setCursor (8, 0);
    lcd.print ("NORMAL  ");
  }

  lcd.setCursor(0, 1);
  {
    lcd.print (temperatureF2);
    lcd.print((char)223);
    lcd.print("F ");
  }

  lcd.setCursor(8, 1);
  // print the days since reset:
  lcd.print("UP");
  lcd.print(millis() * 0.0000000115741);

  // case cooling fan control
  if (temperatureF2 > 75)
  {
    digitalWrite(28, LOW);
  }

  else

  {
    digitalWrite(28, HIGH);
  }
  // End of FanLCD Actions

}

void setup() {

  // timed actions setup
  timer.setInterval(900, BCRead);
  timer.setInterval(10000, FanLCDTask);
  // timer.setInterval (1000, ModeHandler); // Planning to use this for mode handling thread

  // relay pin definitions
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);
  pinMode(RELAY5, OUTPUT);
  pinMode(RELAY9, OUTPUT);
  pinMode(RELAY10, OUTPUT);

  // these relays go on with "LOW" setting HIGH to start
  digitalWrite(RELAY1, HIGH);
  digitalWrite(RELAY2, HIGH);
  digitalWrite(RELAY3, HIGH);
  digitalWrite(RELAY4, HIGH);
  digitalWrite(RELAY5, HIGH);
  digitalWrite(RELAY9, HIGH);
  digitalWrite(RELAY10, HIGH);

  // Start Serial for Console Logging (When connected Temperature Results are NOT reliable)
  Serial.begin(9600);

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  // Managing "Modes" For Xbox, etc.
  BLYNK_WRITE(3)
  {
    int i = param.asInt();
    if (i == 1)
    {
      digitalWrite(RELAY1, HIGH);
      digitalWrite(RELAY2, LOW);
    }
    else
    {
      digitalWrite(RELAY2, HIGH);
      digitalWrite(RELAY1, LOW);
    }
  }

  // Blynk Login
  Blynk.begin(auth);  // Here your Arduino connects to the Blynk Cloud.

  while (Blynk.connect() == false) {
    // Wait until connected
  }
  // This will print Blynk Software version to the Terminal Widget when
  // will log messages here, test code functional
  terminal.println(F("Blynk v" BLYNK_VERSION ": Device started"));
  terminal.println("-------------");
  terminal.println("Type 'Marco' and get a reply, or type");
  terminal.println("anything else and get it printed back.");
  terminal.flush();

}

void loop() {

  Blynk.run();
  timer.run();

}

Thanks for looking at this!

BLYNK_WRITE should be outside any method.

void setup() {
...
}

// Managing "Modes" For Xbox, etc.
BLYNK_WRITE(3)
  {
    int i = param.asInt();
    if (i == 1)
    {
      digitalWrite(RELAY1, HIGH);
      digitalWrite(RELAY2, LOW);
    }
    else
    {
      digitalWrite(RELAY2, HIGH);
      digitalWrite(RELAY1, LOW);
    }
}

void loop() {
  Blynk.run();
  timer.run();
}
1 Like

Also please use one bracket style during coding. Either

void setup() {
...
}

either

void setup() 
{
...
}
2 Likes

You make it look easy, thanks very much, I moved it between setup and loop and it now works, on to play and test.

Thank you, it must hurt your eyes to look at my code. :wink: I will switch to the second style, I like that one better. Next “clean up” I will do just that.

You need to put it outside anything. I usually put it above loop() function, not in setup, not in loop, just as standalone function.

-edit

Too late, lol

2 Likes