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;
}
}