Simple code but partly wont work

Any help appreciated with this simple 4 button code
Objective: Arduino Mega - It has a connected 4 relay board and 2 x RC type servos
PART THAT WORKS OK : if I press buttons 1 or 2 I get a relay activation and the 2 servos move to the pre-determined positions

PART THAT DOES NOT WORK : If I press buttons 3 or 4 I get no relay or servo activity

The button and relay connectivity is good as I can run a simple 4 button/4 relay code without issues.

Although the code compiles OK on the IDE I ran it through an online emulator and got this result:

error report :

sketch.ino: In function 'void setup()':
sketch.ino:16:1: error: a function-definition is not allowed here before '{' token
{
^
sketch.ino:31:1: error: a function-definition is not allowed here before '{' token
{
^

Error during build: exit status 1

Here is the actual code: THANKS ALL..!!

#include <Servo.h>

Servo servo1;
Servo servo2;

int relay1 = 2; //Amp CH1 relay
int relay2 = 3; //Amp CH2 relay
int relay3 = 4; //Amp CH3 relay
int relay4 = 5; //Amp CH4 relay
int button1 = 8; //CH1 Button
int button2 = 9; //CH2 Button
int button3 = 10; //CH3 Button
int button4 = 11; //CH4 Button
void setup()
{
  // Initialize digital output pins for relays:
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
servo1.attach(6); //set pin to servo mode
servo2.attach(7); //set pin to servo mode
}

void loop()
{

  
  bool button1State ;
  button1State = digitalRead(button1);
  if(button1State == LOW)
  {
  
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);
    for (int i = 0; i < 60; i++) {
    servo1.write(i);             
    servo2.write(i);            
    delay(20); //speed of servos 

        
         }
    
  }
    bool button2State ;
  button2State = digitalRead(button2);
  if(button2State == LOW)
  {
   
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, LOW);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);
    for (int i = 0; i < 60; i++) {
    servo1.write(i);             
    servo2.write(i);            
    delay(20); //speed of servos
  }
  bool button3State ;
  button3State = digitalRead(button3);
  if(button3State == LOW)
  {
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, LOW);
    digitalWrite(relay4, HIGH);
    for (int i = 0; i < 60; i++) {
    servo1.write(i);             
    servo2.write(i);            
    delay(20); //speed of servos
  }

  bool button4State ;
  button4State = digitalRead(button4);
  if(button4State == LOW)
  {
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);
     for (int i = 0; i < 60; i++) {
    servo1.write(i);             
    servo2.write(i);            
    delay(20); //speed of servos
  }


  }}}}
1 Like

Post an annotated schematic showing exactly how you have wired it. This must show all power, ground and power sources. Also include links to technical information on each of the hardware devices.

You have a curly brackets misplaced.

If you use IDE autoformat tool (ctrl-t or Tools, Auto format) you should see that the code to read the button3 and button4 are inside the if structure that reads and deals with the button2. In order to read button3 or button4, button2 must be LOW or held down.

Does this work?

#include <Servo.h>

Servo servo1;
Servo servo2;

int relay1 = 2; //Amp CH1 relay
int relay2 = 3; //Amp CH2 relay
int relay3 = 4; //Amp CH3 relay
int relay4 = 5; //Amp CH4 relay
int button1 = 8; //CH1 Button
int button2 = 9; //CH2 Button
int button3 = 10; //CH3 Button
int button4 = 11; //CH4 Button

void setup()
{
   // Initialize digital output pins for relays:
   pinMode(relay1, OUTPUT);
   pinMode(relay2, OUTPUT);
   pinMode(relay3, OUTPUT);
   pinMode(relay4, OUTPUT);
   pinMode(button1, INPUT_PULLUP);
   pinMode(button2, INPUT_PULLUP);
   pinMode(button3, INPUT_PULLUP);
   pinMode(button4, INPUT_PULLUP);
   servo1.attach(6); //set pin to servo mode
   servo2.attach(7); //set pin to servo mode
}

void loop()
{
   bool button1State ;
   button1State = digitalRead(button1);
   if (button1State == LOW)
   {
      digitalWrite(relay1, LOW);
      digitalWrite(relay2, HIGH);
      digitalWrite(relay3, HIGH);
      digitalWrite(relay4, HIGH);
      for (int i = 0; i < 60; i++)
      {
         servo1.write(i);
         servo2.write(i);
         delay(20); //speed of servos
      }
   }
   
   bool button2State ;
   button2State = digitalRead(button2);
   if (button2State == LOW)
   {

      digitalWrite(relay1, HIGH);
      digitalWrite(relay2, LOW);
      digitalWrite(relay3, HIGH);
      digitalWrite(relay4, HIGH);
      for (int i = 0; i < 60; i++)
      {
         servo1.write(i);
         servo2.write(i);
         delay(20); //speed of servos
      }
   } // added this ******************************************
   
   bool button3State ;
   button3State = digitalRead(button3);
   if (button3State == LOW)
   {
      digitalWrite(relay1, HIGH);
      digitalWrite(relay2, HIGH);
      digitalWrite(relay3, LOW);
      digitalWrite(relay4, HIGH);
      for (int i = 0; i < 60; i++)
      {
         servo1.write(i);
         servo2.write(i);
         delay(20); //speed of servos
      }
   } // added this ***************************************
   
   bool button4State ;
   button4State = digitalRead(button4);
   if (button4State == LOW)
   {
      digitalWrite(relay1, LOW);
      digitalWrite(relay2, HIGH);
      digitalWrite(relay3, HIGH);
      digitalWrite(relay4, HIGH);
      for (int i = 0; i < 60; i++)
      {
         servo1.write(i);
         servo2.write(i);
         delay(20); //speed of servos
      }
   }
 // }  comment or remove this *************************
 // } comment or remove this *************************
}
1 Like

Many thanks...your correction worked....my bad!!...for context here is the project it is for - we are aiming to automate the 'grabber' movements on our working replica of the exoskeleton from James Cameron's 'Aliens' movie and are using the arduino for this.... thanks again.

3 Likes

That is very cool. Glad to have helped.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.