Show Posts
Pages: [1] 2 3
1  Using Arduino / Programming Questions / Re: Function calling on: June 23, 2011, 05:42:33 am
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.

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

the running code

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;
   
}
}
2  Using Arduino / Programming Questions / Re: Function calling on: June 23, 2011, 01:25:52 am
the code is not reading this function
Quote
void switchMotor(unsigned int keystate)
.
it works till void loop()

3  Using Arduino / Programming Questions / Function calling on: June 23, 2011, 12:41:17 am
function calling is correct? the code isnt reading the function. it remains in void loop()..

need help..!

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

4  Using Arduino / Programming Questions / Re: Variable or field declared void error on: June 06, 2011, 04:44:07 am
it is unsigned int
5  Using Arduino / Programming Questions / Variable or field declared void error on: June 06, 2011, 01:00:09 am
here is the code
Code:
#include <Servo.h>

Servo myservo1;
Servo myservo2;


const int  buttonPin1 = 2;    // Axthe pin that the pushbutton is attached to
const int  buttonPin1a = 3;  //Bx
const int  buttonPin2 = 4;    //Ay
const int  buttonPin2a = 5;    //By


const int serPin1 = 9;       // the pin that the servo is attached to
const int serPin2 = 10;

int bs1 = 0;         // current state of the button
int bs2 = 0;         // current state of the button
int bs2a = 0; 
int bs1a = 0;

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

void switchMotor(unsigned int keystate);

void setup() {

  int flag1=0;
  int flag2=0;
  myservo1.attach(9);
  myservo2.attach(10);

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

//  // initialize the LED as an output:
//  pinMode(serPin1, OUTPUT);
//  pinMode(serPin2, OUTPUT);

  // initialize serial communication:
  Serial.begin(9600);
}
void loop()
{
  myservo1.attach(9);
  myservo2.attach(10);
//  myservo3.attach(11);
 
  digitalWrite(buttonPin1, HIGH);  // pull-up
  digitalWrite(buttonPin1a, HIGH);
  digitalWrite(buttonPin2, HIGH);
  digitalWrite(buttonPin2a, HIGH);
 
  bs1= digitalRead(buttonPin1);   // LOW = 0 HIGH = 1
  bs1a = digitalRead(buttonPin1a);
  bs2 = digitalRead(buttonPin2);
  bs2a = digitalRead(buttonPin2a);
 


//unsigned int keystate = 0;
 // bitWrite(keystate , 0, digitalRead (buttonPin1));
  //bitWrite(keystate , 1, digitalRead (buttonPin1a));
  //bitWrite(keystate , 2, digitalRead (buttonPin2));
  //bitWrite(keystate , 3, digitalRead (buttonPin2a));

  unsigned int keystate = 0;
  if (bs1 == HIGH)
{
  bitSet(keystate , 0);  // 1 - 1 (BIN)
flag1++;
flag2=0;
}
  if (bs1a == HIGH)
{
bitSet(keystate , 1);  // 2 - 10
flag1=0;
flag2++;
}
  if (bs2 == HIGH) bitSet(keystate , 2);  // 4 - 100
  if (bs2a == HIGH) bitSet(keystate , 3);// 8- 1000
//  if (bs3 == HIGH) bitSet(keystate , 4); // 16- 10000
//  if (bs3a == HIGH) bitSet(keystate , 5);// 32 - 100000
 
  delay(1000);

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


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


void switchMotor(keystate)
{
  switch (keystate )
  {
  case 1: // bs1 pressed others not
      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");
   myservo1.detach();
     delay(1500);
   break;

  case 2: // bs1a pressed others not
    myservo1.write(val1);
    delay(1500);
    Serial.println("B-x");
    myservo1.detach();
    break;
   
  case 4 : // bs2 pressed others not
    myservo2.write(val1);
    delay(1000);
    Serial.println("Ay");
    myservo2.detach();
    break;

  case 8: // bs2a pressed others not
    myservo2.write(val1);
    delay(1000);
    Serial.println("B-y");
    myservo2.detach();
    break;

  case 5: // bs1 and bs2 pressed others not
    myservo1.write(val1);
    delay(1000);
    Serial.println("AA");
     myservo2.write(val1);
    delay(1000);
     myservo1.detach();
    break;
   
   case 10: // bs1 and bs2 pressed others not
    myservo1.detach();
    myservo2.detach();
    delay(1000);
    Serial.println("BB");
    break;
   
   case 9: // bs1-Ax and bs2a-By pressed others not
   Serial.println("AxBy");
    myservo2.write(val1);
    delay(1500);
   

    Serial.println("AxAy");
    myservo1.detach();
    myservo2.detach();
    break;
   
   case 6: // bs1a-Bx and bs2-Ay pressed others not
   Serial.println("BxAy");
    myservo1.write(val1);
    delay(1500);
   

    Serial.println("AxAy");
    myservo1.detach();
    myservo2.detach();
    break;

 case 0: // no keys pressed;
    break;

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


here is the error

Quote
sketch_jun06a:106: error: variable or field 'switchMotor' declared void
sketch_jun06a:106: error: 'keystate' was not declared in this scope

Need Help !


6  Using Arduino / Programming Questions / Re: Servo Rotation control on: June 03, 2011, 04:41:51 am
Thanks I have made these changes.

I want the servo to stop. but as the loop runs over and over again it is reading the input again and again. I have tried using the detach function in Case but doesnot help. smiley-sad
7  Using Arduino / Programming Questions / Re: Servo Rotation control on: June 03, 2011, 04:11:50 am
When the servo is in 0 degree, it is position A
When servo reaches 180 degrees, it is position B

I have placed 2 hall sensors on the block on one face, touching the face of another block. On one block one sensor towards A and another towards B. and on another block magnet towards one end corresponding to the positions of the magnet.

When the magnet is over A-hall sensor, it keep taking input and keep rotating. I want the servo to rotate only once and then not rotate till the magnet changes its position and comes over the hall sensor again
8  Using Arduino / Programming Questions / Servo Rotation control on: June 03, 2011, 03:12:36 am
I am using 2 HX5010 180 degrees rotation motors for my first prototype of modular robotics.
In my project there are cubes which stick to each other and rotate against each other. I am using hall sensors to detect the orientation as well as the rotation.
The motors rotate fine only problem is I am not able to stop the motors in one position. I am unable to find where I am going wrong. If somthing could be done using code or the place ment of sensors are wrong.
Here is my code
Code:
#include <Servo.h>

Servo myservo1;
Servo myservo2;
//Servo myservo3;

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

const int serPin1 = 9;       // the pin that the servo is attached to
const int serPin2 = 10;
//const int serPin3 = 11;

//int lbs1 = 0;     // previous state of the button
//int lbse2 = 0;
//int lbs1a = 0;
//int lbs2a = 0;

int bs1 = 0;         // current state of the button
int bs2 = 0;         // current state of the button
int bs2a = 0; 
int bs1a = 0;
//int bs3 = 0; 
//int bs3a = 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(buttonPin1a, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin2a, INPUT);
//  pinMode(buttonPin3, INPUT);
//  pinMode(buttonPin3a, INPUT);
//  // initialize the LED as an output:
  pinMode(serPin1, OUTPUT);
  pinMode(serPin2, OUTPUT);
//  pinMode(serPin3, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}
void loop()
{
  myservo1.attach(9);
  myservo2.attach(10);
//  myservo3.attach(11);
 
//  digitalWrite(buttonPin1, HIGH);  // pull-up
//  digitalWrite(buttonPin1a, HIGH);
//  digitalWrite(buttonPin2, HIGH);
//  digitalWrite(buttonPin2a, HIGH);
 
  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)
  if (bs1a == HIGH) bitSet(keystate , 1);  // 2 - 10
  if (bs2 == HIGH) bitSet(keystate , 2);  // 4 - 100
  if (bs2a == HIGH) bitSet(keystate , 3);// 8- 1000
//  if (bs3 == HIGH) bitSet(keystate , 4); // 16- 10000
//  if (bs3a == HIGH) bitSet(keystate , 5);// 32 - 100000
// 
  delay(1000);

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

  switch (keystate )
  {
  case 1: // bs1 pressed others not
      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");
   myservo1.detach();
     delay(1500);
   break;

  case 2: // bs1a pressed others not
    myservo1.write(val1);
    delay(1500);
    Serial.println("B-x");
    myservo1.detach();
    break;
   
  case 4 : // bs2 pressed others not
    myservo2.write(val1);
    delay(1000);
    Serial.println("Ay");
    myservo2.detach();
    break;

  case 8: // bs2a pressed others not
    myservo2.write(val1);
    delay(1000);
    Serial.println("B-y");
    myservo2.detach();
    break;

  case 5: // bs1 and bs2 pressed others not
    myservo1.write(val1);
    delay(1000);
    Serial.println("AA");
     myservo2.write(val1);
    delay(1000);
     myservo1.detach();
    break;
   
   case 10: // bs1 and bs2 pressed others not
    myservo1.detach();
    myservo2.detach();
    delay(1000);
    Serial.println("BB");
    break;
   
   case 9: // bs1-Ax and bs2a-By pressed others not
   Serial.println("AxBy");
    myservo2.write(val1);
    delay(1500);
   
//     myservo1.write(val1);
//    delay(1500);
    Serial.println("AxAy");
    myservo1.detach();
    myservo2.detach();
    break;
   
   case 6: // bs1a-Bx and bs2-Ay pressed others not
   Serial.println("BxAy");
    myservo1.write(val1);
    delay(1500);
   
//    myservo2.write(val1);
//    delay(1500);
    Serial.println("AxAy");
    myservo1.detach();
    myservo2.detach();
    break;

 case 0: // no keys pressed;
    break;

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


I want to know if my approach is correct. If not please let me know where am i going wrong.
9  Using Arduino / Programming Questions / Re: number of rotations: servo motor on: April 18, 2011, 06:06:13 am
ok here is my code
Code:
#include <Servo.h>

Servo myservo1;
Servo myservo2;
//Servo myservo3;

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

const int serPin1 = 9;       // the pin that the servo is attached to
const int serPin2 = 10;
//const int serPin3 = 11;

//int lbs1 = 0;     // previous state of the button
//int lbse2 = 0;
//int lbs1a = 0;
//int lbs2a = 0;

int bs1 = 0;         // current state of the button
int bs2 = 0;         // current state of the button
int bs2a = 0; 
int bs1a = 0;
//int bs3 = 0; 
//int bs3a = 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(buttonPin1a, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin2a, INPUT);
//  pinMode(buttonPin3, INPUT);
//  pinMode(buttonPin3a, INPUT);
//  // initialize the LED as an output:
  pinMode(serPin1, OUTPUT);
  pinMode(serPin2, OUTPUT);
//  pinMode(serPin3, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}
void loop()
{
  myservo1.attach(9);
  myservo2.attach(10);
//  myservo3.attach(11);
 
  digitalWrite(buttonPin1, HIGH);  // pull-up
  digitalWrite(buttonPin1a, HIGH);
  digitalWrite(buttonPin2, HIGH);
  digitalWrite(buttonPin2a, HIGH);
 
  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)
  if (bs1a == HIGH) bitSet(keystate , 1);  // 2 - 10
  if (bs2 == HIGH) bitSet(keystate , 2);  // 4 - 100
  if (bs2a == HIGH) bitSet(keystate , 3);// 8- 1000
//  if (bs3 == HIGH) bitSet(keystate , 4); // 16- 10000
//  if (bs3a == HIGH) bitSet(keystate , 5);// 32 - 100000
// 
  delay(1000);

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

  switch (keystate )
  {
  case 1: // bs1 pressed others not
      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");
   myservo1.detach();
     delay(1500);
   break;

  case 2: // bs1a pressed others not
    myservo1.write(val1);
    delay(1500);
    Serial.println("B-x");
    myservo1.detach();
    break;
   
  case 4 : // bs2 pressed others not
    myservo2.write(val1);
    delay(1000);
    Serial.println("Ay");
    myservo2.detach();
    break;

  case 8: // bs2a pressed others not
    myservo2.write(val1);
    delay(1000);
    Serial.println("B-y");
    myservo2.detach();
    break;

  case 5: // bs1 and bs2 pressed others not
    myservo1.write(val1);
    delay(1000);
    Serial.println("AA");
     myservo2.write(val1);
    delay(1000);
     myservo1.detach();
    break;
   
   case 10: // bs1 and bs2 pressed others not
    myservo1.detach();
    myservo2.detach();
    delay(1000);
    Serial.println("BB");
    break;
   
   case 9: // bs1-Ax and bs2a-By pressed others not
   Serial.println("AxBy");
    myservo2.write(val1);
    delay(1500);
   
//     myservo1.write(val1);
//    delay(1500);
    Serial.println("AxAy");
    myservo1.detach();
    myservo2.detach();
    break;
   
   case 6: // bs1a-Bx and bs2-Ay pressed others not
   Serial.println("BxAy");
    myservo1.write(val1);
    delay(1500);
   
//    myservo2.write(val1);
//    delay(1500);
    Serial.println("AxAy");
    myservo1.detach();
    myservo2.detach();
    break;

 case 0: // no keys pressed;
    break;

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


here i want the servo to stop after 1 rotation until another case is called.
10  Using Arduino / Programming Questions / Re: number of rotations: servo motor on: April 18, 2011, 04:51:54 am
ya i posted a part of the code.
actually i am not able to stop the motor at 180 degrees position.
as the conditions are fulfilled in the switch case it is reading HIGH and the rotation is not stopping.
11  Using Arduino / Programming Questions / Re: number of rotations: servo motor on: April 15, 2011, 07:04:46 am
i want to rotate the motor twice with one HIGH signal..here the servo rotates 180 degrees and back to 0 degrees.
12  Using Arduino / Programming Questions / number of rotations: servo motor on: April 15, 2011, 05:11:27 am
How to control the number of rotations of servo motor?

i want to fix on the number of rotations.

I am using putting a part of my code here:
Code:
unsigned int keystate = 0;
  if (bs1 == HIGH) bitSet(keystate , 0);  // 1 - 1 (BIN)
  if (bs1a == HIGH) bitSet(keystate , 1);  // 2 - 10
  if (bs2 == HIGH) bitSet(keystate , 2);  // 4 - 100
  if (bs2a == HIGH) bitSet(keystate , 3);// 8- 1000
  delay(1000);

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

  switch (keystate )
  {
  case 1: // bs1 pressed others not
      myservo1.write(val1);              // tell servo to go to position in variable 'pos'
    delay(1500);                       // waits 15ms for the servo to reach the position
    Serial.println("Ax");
   myservo1.detach();
     delay(1500);
   break;
13  Using Arduino / Programming Questions / Re: too many conditions: help in coding! on: April 12, 2011, 02:36:04 am
Thanks AWOL i went through it.. in my code it(And operation) is happening as i press the buttons. but i want few of the operations as OR.

Another query
i want to switch between cases. but when i am using goto it shows error! smiley-sad

14  Using Arduino / Programming Questions / Re: too many conditions: help in coding! on: April 12, 2011, 01:43:51 am
 Thank s lot robtillaart

I understood it.. I had a doubt.. if i have to carry out bitwise OR what should i do?

as in bs1 OR bs1a?
15  Using Arduino / Programming Questions / Re: too many conditions: help in coding! on: April 08, 2011, 05:41:30 am
here is the code, I tested out like you said..
It is working fine till i read single switches HIGH but it is not reading when 2 switches are HIGH because its reading the previous statements and working accordingly. whats the way out? smiley-sad smiley-sad

Code:
#include <Servo.h>

Servo myservo1;
Servo myservo2;

const int  buttonPin1 = 2;    // the pin that the pushbutton is attached to
const int  buttonPin1a = 4;
const int  buttonPin2 = 7;
const int  buttonPin2a = 6;

const int serPin1 = 9;       // the pin that the servo is attached to
const int serPin2 = 10;
int val= 180;
int val1= 0;
int temp;

int buttonState1, buttonState2,  buttonState2a, buttonState1a = 0;         // current state of the button

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

  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin1a, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin2a, INPUT);

  pinMode(serPin1, OUTPUT);
  pinMode(serPin2, OUTPUT);

  Serial.begin(9600);
}

void loop()
{
  buttonState1 = digitalRead(buttonPin1);
  buttonState1a = digitalRead(buttonPin1a);
  buttonState2 = digitalRead(buttonPin2);
  buttonState2a = digitalRead(buttonPin2a);

  val1=temp;
  temp=val;
  val=val1;
  //if (buttonState1 != lastButtonState1)
  if (buttonState1 == HIGH ) // when button 1(x orientation) pressed motor1 rotates 
  {
    myservo1.write(val);
    delay(1000);
    Serial.println("Ax");
  }
  else{     
    if (buttonState1a == HIGH)
    {
      myservo1.write(val);
      delay(1000);
      Serial.println("B-x");
    }
    else{ 
      if (buttonState2 == HIGH ) // A/B-y 
      {
        myservo2.write(val);
        delay(1000);
        Serial.println("Ay");
      }
       else{

          if (buttonState1 == HIGH && buttonState2 == HIGH)// A-A
          {
            myservo1.write(val);
            delay(1000);
            Serial.println("AA");
          }
    }
  }
}
  }


Pages: [1] 2 3