rotary encoder and flex

I'm using xbee and its shield in arduino mega with 2 encoders and 2 flexs.. i tried to send code using map but it didn't work .. please help!!!!!

Code?

transmitter 
#define encoder0PinA 2
#define encoder0PinB 3
#define encoder0PinC 18
#define encoder0PinD 19
int finger = A0;
int finger1 = A1;

int x=0;
int y=0;


volatile unsigned int encoder0Pos = 0;
volatile unsigned int encoder0Pos1 = 0;
void setup() {
Serial.begin (9600);


pinMode(encoder0PinA, INPUT);
pinMode(encoder0PinB, INPUT);
attachInterrupt(0, doEncoderA, CHANGE);
// encoder pin on interrupt 0 (pin 2)
attachInterrupt(1, doEncoderB, CHANGE);
// encoder pin on interrupt 1 (pin 3)
pinMode(encoder0PinC, INPUT);
pinMode(encoder0PinD, INPUT);
attachInterrupt(4, doEncoderC, CHANGE);
// encoder pin on interrupt 0 (pin 2)
attachInterrupt(5, doEncoderD, CHANGE);
}

void loop(){
  int q = analogRead(A0);
 int w = analogRead(A1);

 if(q){
  int val = map(q, 400, 600, 30, 67);
  // el ar2am elly hatetbe3et ma2soma 3ala 10 ^^^^^^^
      val = constrain(val, 30, 67);
  if((val >= 30) && (val <= 67)){
  Serial.write(val);
 
  delay(40);
} }

if(w){
int val_1 = map(w, 400, 600, 68, 170);
  // el ar2am elly hatetbe3et ma2soma 3ala 10 ^^^^^^^
    val_1 = constrain(val_1, 68, 170);

  if((val_1 >= 68) && (val_1 <=170)){
  Serial.write(val_1);
 
  delay(40);
}}

  }
  
void doEncoderA(){
if (digitalRead(encoder0PinA) == HIGH) {
// found a low-to-high on channel A
if (digitalRead(encoder0PinB) == LOW) {
// check channel B to see which way encoder is turning
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
else // found a high-to-low on channel A
{
if (digitalRead(encoder0PinB) == HIGH) {
// check channel B to see which way encoder is turning
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
//Serial.print(encoder0Pos);
y = map(encoder0Pos,0,1100,380,560); //was >> y = map(encoder0Pos,0,31,0,32)
//========================================

if( (y >= 0) && ( y <= 180) )
{
  
Serial.write(y);
}//===========================================

}
void doEncoderB(){
if (digitalRead(encoder0PinB) == HIGH) {
// found a low-to-high on channel B
if (digitalRead(encoder0PinA) == HIGH) {
// check channel A to see which way
// encoder is turning
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
else // found a high-to-low on channel A
{
if (digitalRead(encoder0PinA) == LOW) {
// check channel B to see which way encoder is turning
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}

y = map(encoder0Pos,0,1100,380,560); //was >> y = map(encoder0Pos,0,31,0,32)
//========================================

if( (y >= 0) && ( y <= 180) )
{

Serial.write(y);
}//===========================================
}
//////////////////////////////////////////////////////////////////////////////////////////////////////

void doEncoderC(){
if (digitalRead(encoder0PinC) == HIGH) {
// found a low-to-high on channel A
if (digitalRead(encoder0PinD) == LOW) {
// check channel B to see which way encoder is turning
encoder0Pos1 = encoder0Pos1 + 1; // CW
}
else {
encoder0Pos1 = encoder0Pos1 - 1; // CCW
}
}
else // found a high-to-low on channel A
{
if (digitalRead(encoder0PinD) == HIGH) {
// check channel B to see which way encoder is turning
encoder0Pos1 = encoder0Pos1 + 1; // CW
}
else {
encoder0Pos1 = encoder0Pos1 - 1; // CCW
}
}

x = map(encoder0Pos1,0,1100,190,370);  //was >> x = map(encoder0Pos1,0,31,66,99)
//============================================

if( (x >= 190) && ( x <= 370) )
{
 
  
Serial.write(x);
}//=================================================


}


void doEncoderD(){
if (digitalRead(encoder0PinD) == HIGH) {
// found a low-to-high on channel B
if (digitalRead(encoder0PinC) == HIGH) {
// check channel A to see which way
// encoder is turning
encoder0Pos1 = encoder0Pos1 + 1; // CW
}
else {
encoder0Pos1 = encoder0Pos1 - 1; // CCW
}
}
else // found a high-to-low on channel A
{
if (digitalRead(encoder0PinC) == LOW) {
// check channel B to see which way encoder is turning
encoder0Pos1 = encoder0Pos1 + 1; // CW
}
else {
encoder0Pos1 = encoder0Pos1 - 1; // CCW
}
}


x = map(encoder0Pos1,0,1100,190,370); //was >> x = map(encoder0Pos1,0,31,66,99)
//================================================

if( (x >= 190) && ( x <= 370) )
{

Serial.write(x);
}//==================================================
}
receiver

#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

int x=0;
int y=0;




void setup() {
Serial.begin (9600);

servo1.attach(9); //Servo 1 is on Pin 9
servo2.attach(10); //Servo 2 is on Pin 10
servo3.attach(11); //Servo 3 is on Pin 11
servo4.attach(12); //Servo 4 is on Pin 12

servo1.write(0); //position servo at 0 initially
servo2.write(0); //position servo at 0 initially
servo3.write(0); //position servo at 0 initially
servo4.write(0); //position servo at 0 initially


}

void loop(){


while(Serial.available() == 0);
  int encoder0Pos = Serial.read();
 
//============================================
  if( (encoder0Pos>=190) && (encoder0Pos<=370) )
  {
   x=map(encoder0Pos,190,370,0,180);

if( (x >= 0) && ( x <= 180) ){
    
servo2.write(x*5.5);
}
}//=================================================

//============================================
if( (encoder0Pos >= 0) && (encoder0Pos <= 32) )
{
y=map(encoder0Pos,380,560,0,180);

  
if( (y >= 0) && ( y <= 180) ){
  
servo1.write(y);
}
}
//////////////////////////////////////
 int out=0;
 int out1=0;
  int data = Serial.read() ;
 
if((data >= 30) && (data <= 67)){
  int pos = map(data*10, 300, 670, 180, 0);
  
    int  pos1 = constrain(pos, 0, 90);// MOHEM GEDDAN
    
    
  if(pos < 90){
  out=pos1*1.75;
  }
  else{
  
  int  pos2 = constrain(pos, 90, 180);// MOHEM GEDDAN
  
  if(pos > 90){
  out=pos2*1.75;
  }
  
   
   }
     servo3.write(out/2);}
  delay(10);
  
  //Serial.println(out);
  
  if((data >= 68) && (data <= 170)){
  int pos = map(data*10, 680, 1700, 180, 0);
  
    int  pos1 = constrain(pos, 0, 90);// MOHEM GEDDAN
    
    
  if(pos < 90){
  out1=pos1;
  }
  else{
  
  int  pos2 = constrain(pos, 90, 180);// MOHEM GEDDAN
  
  if(pos >= 90){
  out1=pos2;
  }
     
 }
  
  
 servo4.write(out1); 
}
  delay(10);
}//=================================================

this for 2 encoders and 2 flex

Aaaaand what's the problem?

  int q = analogRead(A0);
 int w = analogRead(A1);

 if(q){

The analogRead() function returns a value between 0 and 1023. Using that value as a boolean does not make sense.

  int val = map(q, 400, 600, 30, 67);

What if q is not in the range 400 to 600?

      val = constrain(val, 30, 67);
  if((val >= 30) && (val <= 67)){

Clearly, you don’t understand what constrain() does.

I gave up reading at this point:

} }

Put every { on a line by itself.
Put each and every } on a line by itself.
Use Tools + Auto Format to fix that horrid indenting.
Post again.

You seem to be sending random, unidentified values, and expecting the receiver to know what you sent. That will NEVER work.

non of encoders or flex working ... both of encoders and flex working alone but when i but both of them together in the same code they not work

hossamm92:
non of encoders or flex working ... both of encoders and flex working alone but when i but both of them together in the same code they not work

You'll need to explain what "they not work" means. You are doing Serial.write() in an interrupt service routine. That is a REALLY bad idea.

PaulS:
You'll need to explain what "they not work" means. You are doing Serial.write() in an interrupt service routine. That is a REALLY bad idea.

i dont know what to do ... i'm new in using arduino

I gave up reading at this point:

} }

Put every { on a line by itself.
Put each and every } on a line by itself.
Use Tools + Auto Format to fix that horrid indenting.
Post again.

You seem to be sending random, unidentified values, and expecting the receiver to know what you sent. That will NEVER work.
[/quote]
this after auto-format
transmitter

#define encoder0PinA 2
#define encoder0PinB 3
#define encoder0PinC 18
#define encoder0PinD 19
int finger = A0;
int finger1 = A1;

int x = 0;
int y = 0;


volatile unsigned int encoder0Pos = 0;
volatile unsigned int encoder0Pos1 = 0;
void setup() {
 Serial.begin (9600);


 pinMode(encoder0PinA, INPUT);
 pinMode(encoder0PinB, INPUT);
 attachInterrupt(0, doEncoderA, CHANGE);
 // encoder pin on interrupt 0 (pin 2)
 attachInterrupt(1, doEncoderB, CHANGE);
 // encoder pin on interrupt 1 (pin 3)
 pinMode(encoder0PinC, INPUT);
 pinMode(encoder0PinD, INPUT);
 attachInterrupt(4, doEncoderC, CHANGE);
 // encoder pin on interrupt 0 (pin 2)
 attachInterrupt(5, doEncoderD, CHANGE);
}

void loop() {
 int q = analogRead(A0);
 int w = analogRead(A1);

 if (q) {
   int val = map(q, 400, 600, 30, 67);
   // el ar2am elly hatetbe3et ma2soma 3ala 10 ^^^^^^^
   val = constrain(val, 30, 67);
   if ((val >= 30) && (val <= 67)) {
     Serial.write(val);

     delay(40);
   }
 }

 if (w) {
   int val_1 = map(w, 400, 600, 68, 170);
   // el ar2am elly hatetbe3et ma2soma 3ala 10 ^^^^^^^
   val_1 = constrain(val_1, 68, 170);

   if ((val_1 >= 68) && (val_1 <= 170)) {
     Serial.write(val_1);

     delay(40);
   }
 }

}

void doEncoderA() {
 if (digitalRead(encoder0PinA) == HIGH) {
   // found a low-to-high on channel A
   if (digitalRead(encoder0PinB) == LOW) {
     // check channel B to see which way encoder is turning
     encoder0Pos = encoder0Pos + 1; // CW
   }
   else {
     encoder0Pos = encoder0Pos - 1; // CCW
   }
 }
 else // found a high-to-low on channel A
 {
   if (digitalRead(encoder0PinB) == HIGH) {
     // check channel B to see which way encoder is turning
     encoder0Pos = encoder0Pos + 1; // CW
   }
   else {
     encoder0Pos = encoder0Pos - 1; // CCW
   }
 }
 //Serial.print(encoder0Pos);
 y = map(encoder0Pos, 0, 1100, 380, 560); //was >> y = map(encoder0Pos,0,31,0,32)
 //========================================

 if ( (y >= 0) && ( y <= 180) )
 {

   Serial.write(y);
 }//===========================================

}
void doEncoderB() {
 if (digitalRead(encoder0PinB) == HIGH) {
   // found a low-to-high on channel B
   if (digitalRead(encoder0PinA) == HIGH) {
     // check channel A to see which way
     // encoder is turning
     encoder0Pos = encoder0Pos + 1; // CW
   }
   else {
     encoder0Pos = encoder0Pos - 1; // CCW
   }
 }
 else // found a high-to-low on channel A
 {
   if (digitalRead(encoder0PinA) == LOW) {
     // check channel B to see which way encoder is turning
     encoder0Pos = encoder0Pos + 1; // CW
   }
   else {
     encoder0Pos = encoder0Pos - 1; // CCW
   }
 }

 y = map(encoder0Pos, 0, 1100, 380, 560); //was >> y = map(encoder0Pos,0,31,0,32)
 //========================================

 if ( (y >= 0) && ( y <= 180) )
 {

   Serial.write(y);
 }//===========================================
}
//////////////////////////////////////////////////////////////////////////////////////////////////////

void doEncoderC() {
 if (digitalRead(encoder0PinC) == HIGH) {
   // found a low-to-high on channel A
   if (digitalRead(encoder0PinD) == LOW) {
     // check channel B to see which way encoder is turning
     encoder0Pos1 = encoder0Pos1 + 1; // CW
   }
   else {
     encoder0Pos1 = encoder0Pos1 - 1; // CCW
   }
 }
 else // found a high-to-low on channel A
 {
   if (digitalRead(encoder0PinD) == HIGH) {
     // check channel B to see which way encoder is turning
     encoder0Pos1 = encoder0Pos1 + 1; // CW
   }
   else {
     encoder0Pos1 = encoder0Pos1 - 1; // CCW
   }
 }

 x = map(encoder0Pos1, 0, 1100, 190, 370); //was >> x = map(encoder0Pos1,0,31,66,99)
 //============================================

 if ( (x >= 190) && ( x <= 370) )
 {


   Serial.write(x);
 }//=================================================


}


void doEncoderD() {
 if (digitalRead(encoder0PinD) == HIGH) {
   // found a low-to-high on channel B
   if (digitalRead(encoder0PinC) == HIGH) {
     // check channel A to see which way
     // encoder is turning
     encoder0Pos1 = encoder0Pos1 + 1; // CW
   }
   else {
     encoder0Pos1 = encoder0Pos1 - 1; // CCW
   }
 }
 else // found a high-to-low on channel A
 {
   if (digitalRead(encoder0PinC) == LOW) {
     // check channel B to see which way encoder is turning
     encoder0Pos1 = encoder0Pos1 + 1; // CW
   }
   else {
     encoder0Pos1 = encoder0Pos1 - 1; // CCW
   }
 }


 x = map(encoder0Pos1, 0, 1100, 190, 370); //was >> x = map(encoder0Pos1,0,31,66,99)
 //================================================

 if ( (x >= 190) && ( x <= 370) )
 {

   Serial.write(x);
 }//==================================================
}

I gave up reading at this point:

} }

Put every { on a line by itself.
Put each and every } on a line by itself.
Use Tools + Auto Format to fix that horrid indenting.
Post again.

You seem to be sending random, unidentified values, and expecting the receiver to know what you sent. That will NEVER work.

receiver

#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

int x = 0;
int y = 0;




void setup() {
 Serial.begin (9600);

 servo1.attach(9); //Servo 1 is on Pin 9
 servo2.attach(10); //Servo 2 is on Pin 10
 servo3.attach(11); //Servo 3 is on Pin 11
 servo4.attach(12); //Servo 4 is on Pin 12

 servo1.write(0); //position servo at 0 initially
 servo2.write(0); //position servo at 0 initially
 servo3.write(0); //position servo at 0 initially
 servo4.write(0); //position servo at 0 initially


}

void loop() {


 while (Serial.available() == 0);
 int encoder0Pos = Serial.read();

 //============================================
 if ( (encoder0Pos >= 190) && (encoder0Pos <= 370) )
 {
   x = map(encoder0Pos, 190, 370, 0, 180);

   if ( (x >= 0) && ( x <= 180) ) {

     servo2.write(x * 5.5);
   }
 }//=================================================

 //============================================
 if ( (encoder0Pos >= 0) && (encoder0Pos <= 32) )
 {
   y = map(encoder0Pos, 380, 560, 0, 180);


   if ( (y >= 0) && ( y <= 180) ) {

     servo1.write(y);
   }
 }
 //////////////////////////////////////
 int out = 0;
 int out1 = 0;
 int data = Serial.read() ;

 if ((data >= 30) && (data <= 67)) {
   int pos = map(data * 10, 300, 670, 180, 0);

   int  pos1 = constrain(pos, 0, 90);// MOHEM GEDDAN


   if (pos < 90) {
     out = pos1 * 1.75;
   }
   else {

     int  pos2 = constrain(pos, 90, 180);// MOHEM GEDDAN

     if (pos > 90) {
       out = pos2 * 1.75;
     }


   }
   servo3.write(out / 2);
 }
 delay(10);

 //Serial.println(out);

 if ((data >= 68) && (data <= 170)) {
   int pos = map(data * 10, 680, 1700, 180, 0);

   int  pos1 = constrain(pos, 0, 90);// MOHEM GEDDAN


   if (pos < 90) {
     out1 = pos1;
   }
   else {

     int  pos2 = constrain(pos, 90, 180);// MOHEM GEDDAN

     if (pos >= 90) {
       out1 = pos2;
     }

   }


   servo4.write(out1);
 }
 delay(10);
}//=================================================

PaulS:

  int q = analogRead(A0);

int w = analogRead(A1);

if(q){



The analogRead() function returns a value between 0 and 1023. Using that value as a boolean does not make sense.

That is a perfectly acceptable and very common way to test that a value is not zero. I don't know that this is what the OP intended, but it is very common practice for those of us who actually write code.

Delta_G:
That is a perfectly acceptable and very common way to test that a value is not zero.

So where is the problem in the code ?

but it is very common practice for those of us who actually write code.

I actually write code for a living, and I NEVER do that.

if(i != 0)

is not that difficult to type, and makes it CRYSTAL CLEAR what is being compared to what.

PaulS:
I actually write code for a living, and I NEVER do that.

if(i != 0)

is not that difficult to type, and makes it CRYSTAL CLEAR what is being compared to what.

The language specification makes it crystal clear what that is supposed to do.