Function calling

function calling is correct? the code isnt reading the function. it remains in void loop()…

need help…!

#include <Servo.h>



Servo myservo1;
Servo myservo2;
Servo myservo3;


const int  buttonPin1 = 2;    // Ax the pin that the sensor is attached to
const int  buttonPin1a = 5;  //Bx
const int  buttonPin2 = 3;    //Ay
const int  buttonPin2a = 6;    //By
const int  buttonPin3 = 4;    //Az
const int  buttonPin3a = 7;    //Bz


int bs1 = 0;         // current state of the button
int bs2 = 0;         // Sensor on fixed face
int bs2a = 0;        // Sensor on motor face
int bs1a = 0;
int bs3 = 0;         // current state of the button
int bs3a = 0;  

int flag1=0;        // counter for sensor- motor 1
int flag2=0;
int flag3=0;
 

int val= 180;
int val1= 0;
int temp;
int pos=0;

void switchMotor(unsigned int keystate);

void setup() {

  
  myservo1.attach(9);
  myservo2.attach(10);
  myservo3.attach(11);

  // initialize the button pin as a input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin1a, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin2a, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin3a, INPUT);

 // initialize serial communication:
  Serial.begin(9600);
}
void loop()
{
   
  bs1= digitalRead(buttonPin1);   // LOW = 0 HIGH = 1
  bs1a = digitalRead(buttonPin1a);
  bs2 = digitalRead(buttonPin2);
  bs2a = digitalRead(buttonPin2a);
  bs3 = digitalRead(buttonPin3);
  bs3a = digitalRead(buttonPin3a);
  
 unsigned int keystate = 0;
  if (bs1 == HIGH)
	{
 	bitSet(keystate , 0);  // 1 - 1 (BIN)
	flag1++;
	}
  if (bs2 == HIGH) ;  // 2 - 10
        {
 	bitSet(keystate , 1);  // 10 - 2 (BIN)
	flag2++;
	}
//  if (bs2a == HIGH) bitSet(keystate , 3);// 8- 1000

   if (bs3 == HIGH) ;  // 4 - 100
         {
 	bitSet(keystate , 2);  // 100 - 3 (BIN)
	flag3++;
	}
//  if (bs3a == HIGH) bitSet(keystate , 3);// 8- 1000
  
  
 

  Serial.print("STATE: "); 
  Serial.println(keystate, BIN);  // 0-15 = 0 - 1111
   delay(1000);
  temp= val;
  val = val1;
  val1 = temp;

	
if(flag1==1) switchMotor( keystate );
if(flag2==1) switchMotor( keystate );
if(flag3==1) switchMotor( keystate );
if(flag1==1 && flag2==1) switchMotor( keystate );
if(flag2==1 && flag3==1) switchMotor( keystate );
if(flag3==1 && flag1==1) switchMotor( keystate );
if(flag3==1 && flag1==1 && flag2==1) switchMotor( keystate );

}
	

void switchMotor(unsigned int keystate)
{
  switch (keystate )
  {
  case 1: // Sensor opp M1 ON(fixed)
      myservo1.write(val1);              // tell servo to go to position in variable 'pos' 
    delay(1500);                       // waits for the servo to reach the position 
    Serial.println("Ax");
    break;

  case 2: // Sensor opp M2 ON(fixed)
    myservo1.write(val1);
    delay(1500);
    Serial.println("Ay");
    break;
    
  case 4 : // Sensor opp M3 ON(fixed)
    myservo3.write(val1);
    delay(1000);
    Serial.println("Az");
    break;

  case 3: // Sensor1 &2 ON (fixed)
  if(bs1a== LOW && bs2a== LOW)       // Sensors on the Motor face
  {
    myservo1.detach();
    myservo2.detach();
  }
  else
  {
    if (bs1a== LOW && bs2a== HIGH)   
     { myservo1.write(val1);
       myservo2.write(val1);
     }
   else
   {
     if(bs2a==LOW && bs1a== HIGH)
     { myservo2.write(val1);
        myservo1.write(val1);
      }
    
   }
  }
  
    delay(1000);
    Serial.println("B-y");
     break;

  case 5: 
    if(bs1a== LOW && bs3a== LOW)     // Sensors on the Motor face
  {
    myservo1.detach();
    myservo3.detach();
  }
 else
  {
    if (bs1a== LOW && bs3a== HIGH)
     { myservo1.write(val1);
       myservo3.write(val1);
     }
   else
   {
     if(bs3a==LOW && bs1a== HIGH)
     { myservo3.write(val1);
        myservo1.write(val1);
      }
    
   }
  }
  
    delay(1000);
    Serial.println("B-y");
     break;
    
    
    case 6: 
    if(bs2a== LOW && bs3a== LOW)    // Sensors on the Motor face
  {
    myservo2.detach();
    myservo3.detach();
  }
  else
  {
    if (bs2a== LOW && bs3a== HIGH)
     { myservo2.write(val1);
       myservo3.write(val1);
     }
   else
   {
     if(bs3a==LOW && bs2a== HIGH)
     { myservo3.write(val1);
        myservo2.write(val1);
      }
    
   }
  }
  
    delay(1000);
    Serial.println("B-y");
     break;
    
   case 7: // bs1 and bs2 pressed others not
    myservo1.detach();
    myservo2.detach();
    myservo3.detach();
    delay(1000);
    Serial.println("Kill Loop");
    break;
    
 case 0: // no keys pressed;
    break;

  default:  // invalid key combination
    // error message
    Serial.println("Invalid key combination pressed");
    break;
    
    
  }
}

the code is not reading this function

void switchMotor(unsigned int keystate)

.
it works till void loop()

Try deleting this before setup
void switchMotor(unsigned int keystate)

Then in the 2nd instance, take out the "unsigned int", I think that could be creating a new variable to be defined, wiping out the variable you are trying to pass in.
If you put a print statement in here
case 0: // no keys pressed;
break;

I think you could confirm that.
Put 'unsigned int keystate' in the presetup listing with the other variables, and check that it is not defined again elsewhere in your code.

Just a guess, haven't done any functions myself.

Try deleting this before setup
void switchMotor(unsigned int keystate)

It’s a function prototype, and is perfectly valid to have there. The Arduino IDE will create one if there is not one present.

Then in the 2nd instance, take out the “unsigned int”, I think that could be creating a new variable to be defined, wiping out the variable you are trying to pass in.

What “unsigned int”? In the function declaration statement? That one MUST be there. It defines the type of argument.

Put ‘unsigned int keystate’ in the presetup listing with the other variables, and check that it is not defined again elsewhere in your code.

Making a global variable that gets used, rather than local variables being passed to functions, is not the answer.

If you put a print statement in here
case 0: // no keys pressed;
break;

I think you could confirm that.

You could confirm that the function is being called, so this is good advice.

Just a guess, haven’t done any functions myself.

Then you should refrain from offering advice.

I think that the real problem is that the switches are not being read properly. You are not enabling the internal pullup resistors, so, this implies that you have external pull-up or pull-down resistors connected with the switches. The fact that you are expecting the switch to be HIGH when pressed implies pull-down resistors. Do you, in fact, have pull-down resistors wired with the switches? How ARE the switches wired?

if (bs2 == HIGH) ;

Oopsie.

I have put pull up resistors with the switches. thats why i havent included it in the code.

The code is running fine now.
made this change. The code could not read because of absence of curly braces. now i have added it.

  if(flag1==1) 
  {
  switchMotor( keystate );
 }
  if(flag2==1)
  {
  switchMotor( keystate );
  }
  if(flag1==1 && flag2==1) 
  {
  switchMotor( keystate );
 }

the running code

#include <Servo.h>
void switchMotor(unsigned int keystate);

Servo myservo1;
Servo myservo2;
Servo myservo3;


#define  buttonPin1 2    // Ax the pin that the sensor is attached to
const int  buttonPin1a = 5;  //Bx
#define buttonPin2 3    //Ay
const int  buttonPin2a = 6;    //By
const int  buttonPin3 = 4;    //Az
const int  buttonPin3a = 7;    //Bz


int bs1 = 0;         // current state of the button
int bs2 = 0;         // Sensor on fixed face
int bs2a = 0;        // Sensor on motor face
int bs1a = 0;
int bs3 = 0;         // current state of the button
int bs3a = 0;  

int flag1=0;        // counter for sensor- motor 1
int flag2=0;
int flag3=0;
 

int val= 180;
int val1= 0;
int temp;
int pos=0;

void setup()
{
  myservo1.attach(9);
  myservo2.attach(10);
  myservo3.attach(11);

  // initialize the button pin as a input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
 // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  unsigned int keystate = 0;
  if(digitalRead(buttonPin1)==HIGH)
  {
    (bitSet(keystate , 0));
    flag1++;
     Serial.println(flag1, DEC);
     delay(1000);
  }
  if(digitalRead(buttonPin2)==HIGH)
   {
    (bitSet(keystate , 1));
    flag2++;
     Serial.println(flag2, DEC);
     delay(1000);
  }
   Serial.print("STATE: "); 
  Serial.println(keystate, BIN);  // 0-15 = 0 - 1111
   delay(1000);
   if (keystate==0)
   {
     flag1=0;
     flag2=0;
     flag3=0;
   }
  temp= val;
  val = val1;
  val1 = temp;
  if(flag1==1) 
  {
  switchMotor( keystate );
  Serial.print("switchMotor1 "); 
}
  if(flag2==1)
  {
  switchMotor( keystate );
  Serial.print("switchMotor2 "); 
}
  if(flag1==1 && flag2==1) 
  {
  switchMotor( keystate );
  Serial.print("switchMotor3 "); 
}
  
}

void switchMotor(unsigned int keystate)
{
  temp= val;
  val = val1;
  val1 = temp;

 switch (keystate )
  {
  case 1: // 1 Sensor opp M1 ON(fixed)
      myservo1.write(val1);              // tell servo to go to position in variable 'pos' 
    delay(1500);                       // waits for the servo to reach the position 
    Serial.println("Ax");
    break;

  case 2: // 10 Sensor opp M2 ON(fixed)
    myservo2.write(val1);
    delay(1500);
    Serial.println("Ay");
    break;
    
  case 4 : // 100 Sensor opp M3 ON(fixed)
    myservo3.write(val1);
    delay(1000);
    Serial.println("Az");
    break;

  case 3: //11 Sensor1 &2 ON (fixed)
  if(bs1a== LOW && bs2a== LOW)       // Sensors on the Motor face
  {
    myservo1.detach();
    myservo2.detach();
    Serial.println("2A1");
  }
  else
  {
    if (bs1a== LOW && bs2a== HIGH)   
     { myservo1.write(val1);
       myservo2.write(val1);
     }
   else
   {
     if(bs2a==LOW && bs1a== HIGH)
     { myservo2.write(val1);
        myservo1.write(val1);
        Serial.println("2A2");
      }
    
   }
  }
  
    delay(1000);
    Serial.println("B-y");
     break;
     
     case 0: // no keys pressed;
    break;

  default:  // invalid key combination
    // error message
    Serial.println("Invalid key combination pressed");
    break;
    
}
}