Go Down

Topic: quad-encoder // comparing strings (Read 395 times) previous topic - next topic

ritzdank

Aug 01, 2006, 03:41 am Last Edit: Aug 01, 2006, 03:41 am by ritzdank Reason: 1
hi!

i´m working on a quad-encoder: http://www.mcmanis.com/chuck/robotics/projects/lab-x3/quadratrak.html
something like this. as test surrounding i took 2 buttons and press them by hand.

i´ve encountered a problem with string comparison or with bit-wise comparison

Code: [Select]
int ledPin = 13;

int photopin1 = 7;
int photopin2 = 8;

int value1 = 0;
int value2 = 0;
int value = 0;
char stateprevious = '00';
char statenow = '00';
int rs232val = 0;
int delaystate = HIGH;


void setup() {
pinMode(photopin1, INPUT);
pinMode(photopin2, INPUT);
pinMode(ledPin, OUTPUT);
beginSerial(19200);
}
void check_state()  {
delaystate = !delaystate;

if (value1 == HIGH && value2 == HIGH)  {
  if (delaystate == HIGH)  {
      stateprevious = '11';           }
  else {
     statenow = '11';
       }
}
else if (value1 == LOW && value2 == HIGH)  {
      if (delaystate == HIGH)  {
      stateprevious = '01';     }
  else {
     statenow = '01';
  }
}
 else if (value1 == HIGH && value2 == LOW)  {
   if (delaystate == HIGH)  {
      stateprevious = '10';     }
  else {
     statenow = '10';
  }
}
 else if (value1 == LOW && value2 == LOW)  {
   if (delaystate == HIGH)  {
      stateprevious = '00';     }
  else {
     statenow = '00';
  }
}
}

void loop() {
value1 = digitalRead(photopin1);
value2 = digitalRead(photopin2);
check_state();
 
  serialWrite(statenow);
  serialWrite(10);
  serialWrite(13);
  serialWrite(stateprevious);
  serialWrite(10);
  serialWrite(13);

}


in serial monitor i cant see the correct state. it only prints one character per line?!?!!

CasNet

Hi,

It's been a long time since you posted this question, but the main problem with the attached code is the use of char variables. You are trying to assign values like '00' to a single byte char variable. This is a very bad thing to do because '00' has 2 bytes and the extra byte will either get lost or cause a tear in the space-time continuum -- either of which is very bad.  ;)

Regards,
David

Go Up