Pages: [1]   Go Down
Author Topic: quad-encoder // comparing strings  (Read 352 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 36
blablablabla
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?!?!!
« Last Edit: July 31, 2006, 08:41:51 pm by ritzdank » Logged

California
Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley-wink

Regards,
David
Logged

Pages: [1]   Go Up
Jump to: