0

I have a RFID reader which is connected to the Arduino.. and successfully receives tag number to serial monitor. But when I put the LED blink inside the loop, then the rfid reading is delayed: it receives tag numbers one by one after each blink.

Code is below. I think I should use multi-threading or interrupts, but I don't know how to do it.

void setup() {

   Serial.begin(9600);
   Serial1.begin(9600);
   pinMode(2, OUTPUT);

}

void loop() {

    // blink led in pin 2
    digitalWrite(2, HIGH);
    delay(1000);
    digitalWrite(2, LOW);
    delay(1000);

//Below Serial1.available() checks my rfid reader input signal and print on serial monitor of Arduino.. and Serial1 is on pin RX1 19 (RX1 19 is the default pin for serial1 defined by Arduino developers "http://arduino.cc/en/reference/serial#.Uyvbq6iSzIg") to which my rfid reader is connected

   if (Serial1.available()) {

      int i = Serial1.read();
      Serial.print(i);

     }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
sham999
  • 121
  • 1
  • 3
  • 12

2 Answers2

3

There is an Arduino file called "Blink without Delay" It is included with the Arduino examples and I have linked to it. Do yourself a favor and take ten minutes to read through the tutorial and the code in order to understand what it does.

spring
  • 18,009
  • 15
  • 80
  • 160
  • In my case I need to blink the led with delay not without delay while reading the rfid tag. How can I do It ? please advice – sham999 Mar 21 '14 at 09:28
  • 2
    @sham999 - If you can make that comment, you either didn't read or didn't understand the "Blink without Delay" tutorial. If you didn't understand it, what didn't you understand? If you didn't read it, my freelance rate is $100/hr. I will happily write your code for you. – spring Mar 21 '14 at 13:26
  • I read it. I want to turn on the led for 1 second and turn it off.. just for 1 second it should be lighted.. How can I do it with blink without delay? – sham999 Mar 21 '14 at 15:20
0

You can use the timer of the Arduino and the appropriate interrupts. So something like this here:

void setup() {
    Serial.begin(9600);
    Serial1.begin(9600);
    pinMode(2, OUTPUT);
    /* Initialize timer, e.g. timer1 */
    noInterrupts();    //disable interrupts
    TCCR1A = 0;        //Timer1 control register A
    TCCR1B = 0;        //Timer1 control register B
    TCNT1 = 0;         //Counter of timer1
    OCR1A = 15625;     //Timer1 compare match register

    TCCR1B |= (1<<WGM12);            //Set timer1 in CTC mode
    /* Define prescaler with 1024. If the clock is 16 MHz, then timer1 will run
     * with 16 MHz / 1024 = 15625 Hz --> 64 us, to get one second, we
     * need 15625 cycles, which is the OCR1A register */
    TCCR1B |= (1<<CS10) | (1<<CS12); // clock prescaler = 1024

    TIMSK1 |= (1<<OCIE1A);    //enable timer compare interrupt
    interrupts();             //enable interrupts
}

//This routine is called every time, timer1 counter reaches the compare register
ISR(Timer1_COMPA_vect) {
    digitalWrite(2, digitalRead(2) ^ 1);    //toggle LED
}

//the loop
void loop() {
    if (Serial1.available()) {
        int i = Serial1.read();
        Serial.print(i);
    }
}
pizzaani
  • 332
  • 1
  • 9
  • Sir this code is not working.. it just reads the rfid tags. but led does not blink. Can you help me – sham999 Mar 21 '14 at 18:46
  • If blinking does not work, could you check your timer settings? What is your default speed? What happens if you change the value of OCR1A to low values? – pizzaani Mar 22 '14 at 18:00
  • as far I have achieved many things with the help of pizzaani that I could not do.. But sir now my issue is, How can I get two values sent from java to the arduino and process them separately inside the arduino? please help me.. I have described the isse in this link "http://stackoverflow.com/questions/22644644/java-how-to-pass-multiple-parameters-over-serial-port-to-arduino-mega" Please help me Pizzaani – sham999 Mar 26 '14 at 05:02
  • You could help me too by voting my answers. – pizzaani Mar 26 '14 at 16:35
  • I will happily do that... Myu project is 99% succcessfull, only GSM section has some erros. I have programmed the Arduino very well.. I use sim900 gsm module.. I have explained the issue in the following link.. Please visit that and help me pizzaani http://stackoverflow.com/questions/22877262/arduino-sending-sms-in-gsm-sim900-error-please-help-me-gyus – sham999 Apr 06 '14 at 03:00