Go Down

Topic: Need help combining two sketches, (braces failure) (Read 455 times) previous topic - next topic

JohannesTN

Hi again forum

I'm back with another problem, i have two sketches which work perfectly individually, but when i try to combine them, i get this error:
Quote
Skab:39: error: expected unqualified-id before '{' token
I know it has something to do with my braces, but i can't figure out what's wrong.

sketch 1:
Code: [Select]
char val;         // Variabel, modtager data fra serielport
int statusled = 13;  // Satus LED
int solenoid = 2; // kraftfelt

void setup()
{
  pinMode(statusled, OUTPUT);  // pin 13 (status LED) defineret som output
  pinMode(solenoid, OUTPUT); // pin 2 (kraftfelt) defineret som output

  Serial.begin(9600);       // start serial kommunikation med baudrate på 9600bps

}

void loop() {
  if( Serial.available() )       // Hvis der er data at læse
  {;}
    val = Serial.read();         // Læs det, og gem det i variablen 'val'

  if( val == 's' )               // hvis 's' modtages, sluk status LED og kraftfelt

  {
   digitalWrite(statusled, LOW);    // sluk status LED
   digitalWrite(solenoid, LOW);     // sluk kraftfelt
delay(1000);                  // vent et sekund   
Serial.println("Skab låst ");
  }

if( val == 't' )               // hvis 't' modtages, tænd status LED og kraftfelt
{
    digitalWrite(statusled, HIGH);  // tænd status LED
    digitalWrite(solenoid, HIGH);  // tænd Kraftfelt
    delay(1000);                  // vent et sekund
    Serial.println("Skab åbnet ");
  }
}


Sketch 2:
Code: [Select]
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:   
    digitalWrite(ledPin, HIGH); 
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}


Last but not least, my try to combine them:
Code: [Select]
char val;         // Variabel, modtager data fra serielport
int statusled = 13;  // Satus LED
int solenoid = 2; // kraftfelt
const int buttonPin = 3;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status

void setup()
{
  pinMode(statusled, OUTPUT);  // pin 13 (status LED) defineret som output
  pinMode(solenoid, OUTPUT); // pin 2 (kraftfelt) defineret som output
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);       // start serial kommunikation med baudrate på 9600bps

}

void loop()
{
  if( Serial.available() )       // Hvis der er data at læse
  {;}
    val = Serial.read();         // Læs det, og gem det i variablen 'val'

  if( val == 's' )               // hvis 's' modtages, sluk status LED og kraftfelt

  {
   digitalWrite(statusled, LOW);    // sluk status LED
   digitalWrite(solenoid, LOW);     // sluk kraftfelt
delay(1000);                  // vent et sekund   
Serial.println("Skab låst ");
  }

if( val == 't' )               // hvis 't' modtages, tænd status LED og kraftfelt
{
    digitalWrite(statusled, HIGH);  // tænd status LED
    digitalWrite(solenoid, HIGH);  // tænd Kraftfelt
    delay(1000);                  // vent et sekund
    Serial.println("Skab åbnet ");
  }
}
{
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:   
    digitalWrite(statusled, HIGH); 
  }
  else {
    // turn LED off:
    digitalWrite(statusled, LOW);
  }
}


I think my problem is to find, somewhere here:
Code: [Select]
{
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:   
    digitalWrite(statusled, HIGH); 
  }
  else {
    // turn LED off:
    digitalWrite(statusled, LOW);
  }
}


I hope someone can point out the problem with the combined sketch, because i can't do it myself. Thank you very much, I appreciate your help

best regards
Johannes

JohannesTN

nwm, changed my sketch to this, and now it is working, sorry for your time :-)

Code: [Select]
char val;         // Variabel, modtager data fra serielport
int statusled = 13;  // Satus LED
int solenoid = 2; // kraftfelt
const int buttonPin = 3;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status

void setup()
{
  pinMode(statusled, OUTPUT);  // pin 13 (status LED) defineret som output
  pinMode(solenoid, OUTPUT); // pin 2 (kraftfelt) defineret som output
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);       // start serial kommunikation med baudrate på 9600bps

}

void loop()
{
  if( Serial.available() )       // Hvis der er data at læse
  {;}
    val = Serial.read();         // Læs det, og gem det i variablen 'val'

  if( val == 's' )               // hvis 's' modtages, sluk status LED og kraftfelt

  {
   digitalWrite(statusled, LOW);    // sluk status LED
   digitalWrite(solenoid, LOW);     // sluk kraftfelt
delay(1000);                  // vent et sekund   
Serial.println("Skab låst ");
  }

if( val == 't' )               // hvis 't' modtages, tænd status LED og kraftfelt
{
    digitalWrite(statusled, HIGH);  // tænd status LED
    digitalWrite(solenoid, HIGH);  // tænd Kraftfelt
    delay(1000);                  // vent et sekund
    Serial.println("Skab åbnet ");
  }
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:
    delay(2000);   
    digitalWrite(solenoid, LOW); 
  }
  else {
    // turn LED off:
    digitalWrite(solenoid, HIGH);
  }
}

PaulS

Check out what Tools + Auto Format does to your sketch. I think you'll like it. I know I would have preferred that you use it BEFORE posting code.

GoForSmoke

Only question; what the heck is that {;} supposed to do? Why isn't the serial read -inside- the braces for the if serial available?

But you say it works....

Quote

sketch 1:
Code: [Select]

void loop() {
  if( Serial.available() )       // Hvis der er data at læse
  {;}
    val = Serial.read();         // Læs det, og gem det i variablen 'val'


Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy