Pages: [1]   Go Down
Author Topic: Need help combining two sketches, (braces failure)  (Read 317 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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:
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:
{
  // 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
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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);
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49033
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4801
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void loop() {
  if( Serial.available() )       // Hvis der er data at læse
  {;}
    val = Serial.read();         // Læs det, og gem det i variablen 'val'
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: [1]   Go Up
Jump to: