RFID and H-Bridge Integration

12 Nov 2010

Hello Again Guys,

I am pretty rusty at my programming language and am trying to get by using the example codes. My goal is to utilize my rfid input signal to rotate a motor one way, and once the motor is completed it's rotation it stops and waits for another rfid signal to rotate the opposite direction. The problem that I'm having is that it skips straight to the reverse mode and does not go back to the forward direction. Can anyone help debug this flaw in programming?

 

// Library importation needs to take place to use functions
#include "mbed.h"
#include "Motor.h"
#include "ID12RFID.h"

Motor dcMotor(p23, p11, p12);
ID12RFID rfid(p14);
Serial pc(USBTX, USBRX);

// Declare output pins for test
DigitalOut myled(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
DigitalOut myled4(LED4);



// Main Program Syntax
int main() {

    myled = 0;
    
    while (1) {

        // locking mode
        if (myled = 0 && (rfid.readable())){
                pc.printf("RFID Tag number : %d\n", rfid.read());
            
            // Go from full speed forward, to full speed reverse
            for (float speed=0.4; speed > -1.0 ; speed -= 0.1) {
                dcMotor.speed(speed);
                // state 2 in forward
                myled2 = 1;     
                pc.printf("I'm in Forward");
                wait(0.05);
            }
            myled = myled + 1;
        }    
        // unlocking mode
        else if (myled = 1 && (rfid.readable())){
                pc.printf("RFID Tag number : %d\n", rfid.read());
                
        // Go from full speed reverse, to full speed forward
        for (float speed=-0.4; speed < 1.0 ; speed += 0.1) {
            dcMotor.speed(speed);
            // state 3 in reverse
            myled3 = 1;     // on
            pc.printf("I'm in Reverse");
            wait(0.05);
        }
         myled = myled - 1;
    }

    }
}

12 Nov 2010

Hi,

I think the problem lies with your use of = and &&. Try if (myled == 0 & (rfid.readable())){ and else if (myled = 1 && (rfid.readable())){
 At the moment you are doing an assignment and a LOGICAL AND operation instead or an equal to and a bitwise AND.

I would have thought the complier would have thrown up a warning about this, it usually does when I get them muddelled up!

Also you might be better off assigning 0 or 1 to myled instead of myled = myled - 1; as this could result in myled becoming -1 which would make the program hang.

15 Nov 2010

Thanks for your help Martin, I will try the changes and hopefully a good outcome. Thanks Again

Henry