Go Down

Topic: motorcycle blinkers /rfid (Read 290 times) previous topic - next topic

migl

Hi!

sorry but im really new to developing and arduino and im having trouble to complete my code with success.

Im doing a motorcycle rfid ignition starter and 2 blinkers , the rfid part i have it sorted but the blinker part is getting really hard...

for now i have one pushbutton to start the blinking cycle but i dont know how to get out of it

ill paste the code bellow and if theres any good samaritan willing to lend me 2 minutes i would be very grateful

Thanks

Miguel


/*
 * ----------------------------------------------------------------------------
 * This sketch uses the MFRC522 library ; see https://github.com/miguelbalboa/rfid
 * for further details and other examples.
 *
 * NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
 *
 * This sketch show a simple locking mechanism using the RC522 RFID module.
 * ----------------------------------------------------------------------------
 * Typical pin layout used:
 * -----------------------------------------------------------------------------------------
 *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD   Uno           Mega      Nano v3    Leonardo/Micro   Pro Micro
 * Signal      Pin          Pin           Pin       Pin        Pin              Pin
 * -----------------------------------------------------------------------------------------
 * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
 * SPI SS      SDA(SS)      10            53        D10        10               10
 * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
 * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
 * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
 *
 */
#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above
#define RELAY_ON 0
#define RELAY_OFF 1
#define Relay_1  2  // Arduino Digital I/O pin number
#define Relay_2  3  // Arduino Digital I/O pin number
#define Relay_3  4  // Arduino Digital I/O pin number

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

String read_rfid;
String ok_rfid_1="a52ccd65";
String ok_rfid_2="65b51453";
String ok_rfid_3="ec217b25"; //String ok_rfid_2="ffffffff"; //add as many as you need.

const int buttonleft = 6;
const int buttonright = 7; //input digital 8 ta livre
int statel = HIGH;      // the current state of the output pin
int readingl;           // the current reading from the input pin
int previousl = LOW;    // the previous reading from the input pin
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers


/*
 * Initialize.
 */
void setup() {
  digitalWrite(Relay_1, HIGH);
  digitalWrite(Relay_2, HIGH);
  digitalWrite(Relay_3, HIGH);
  pinMode(Relay_1, OUTPUT);
  pinMode(Relay_2, OUTPUT); 
  pinMode(Relay_3, OUTPUT); 
  pinMode(buttonleft, INPUT);
  pinMode(buttonright, INPUT);
  delay(1000); //Check that all relays are inactive at Reset

//      Serial.begin(9600);         // Initialize serial communications with the PC
//    while (!Serial);            // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
    SPI.begin();                // Init SPI bus
    mfrc522.PCD_Init();         // Init MFRC522 card
}

/*
 * Helper routine to dump a byte array as hex values to Serial.
 */
void dump_byte_array(byte *buffer, byte bufferSize) {
    read_rfid="";
    for (byte i = 0; i < bufferSize; i++) {
        read_rfid=read_rfid + String(buffer, HEX);
    }
}

void open_lock() {
  digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
}

void pisca_left() {
   digitalWrite(Relay_2, LOW);// set the Relay ON
   delay(450);
   digitalWrite(Relay_2, HIGH);// set the Relay OFF
   delay(450);
}

void pisca_right() {
   digitalWrite(Relay_3, LOW);// set the Relay ON
   delay(1000);
   digitalWrite(Relay_3, HIGH);// set the Relay OFF
   delay(1000);
}

void pisca_panic() {
   digitalWrite(Relay_3, LOW);// set the Relay ON
   digitalWrite(Relay_2, LOW);// set the Relay ON
   delay(1000);
   digitalWrite(Relay_3, HIGH);// set the Relay OFF
   digitalWrite(Relay_2, HIGH);// set the Relay OFF
   delay(1000);
}


void loop()
{
  readingl = digitalRead(buttonleft);
  int stateRelay = digitalRead(Relay_1);
if (stateRelay == HIGH)
    {  // Look for new cards
    if ( ! mfrc522.PICC_IsNewCardPresent())
        return;

    // Select one of the cards
    if ( ! mfrc522.PICC_ReadCardSerial())
        return;

    dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
    Serial.println(read_rfid);
    if (read_rfid==ok_rfid_1 || read_rfid==ok_rfid_2 || read_rfid==ok_rfid_3)
      //ok, open the door.
        open_lock();
      }
  else if (readingl == HIGH && previousl == LOW && millis() - time > debounce) {
    if (statel == HIGH){
      while (previousl == LOW)
      pisca_left();
     statel = LOW;
     Serial.println(statel);
           }  else  {
        statel = HIGH;{
       }
      }
    time = millis();   
  }

   digitalWrite(Relay_2, statel);
    previousl = readingl;

      }
     
   

AWOL

And now with plus code tags and minus italics, please.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.


DVDdoug

It's going to be a LOT easier to develop your code if you figure-out the blinking without the RFID stuff.  And keep it as simple as possible...  Start with just one blinker activated by a switch and add the other one after the first one works...


Once all of your "software modules" work, you can put everything together.

Look at the Blink Without Delay example and the post at the top of the forum that says Demonstration code for doing several things at the same time.    

Delays generally mess everything up because you can't check the state of the inputs or do anything else during the delay time.  And if you've got a delay in a loop the processor is stuck in a delay most of the time...

You could probably do it with delay() but you'd have to go into a left-blinker or right-blinker while() loop and stay in the loop until the turn-indicator switch is de-activated.

migl

Thanks for the reply DVDdoug

I did it by blocks as you sugested thats how i get here.

Meanwhile i managed to put one side working but now i have no idea where to put the second one.

And its starting blinking after it accepts the rfid tag

Sorry AWOL like i said im new to this.


Code: [Select]

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above
#define RELAY_ON 0
#define RELAY_OFF 1
#define Relay_1  2  // Arduino Digital I/O pin number
#define Relay_2  3  // Arduino Digital I/O pin number
#define Relay_3  4  // Arduino Digital I/O pin number

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

String read_rfid;
String ok_rfid_1="a52ccd65";
String ok_rfid_2="65b51453";
String ok_rfid_3="ec217b25"; //String ok_rfid_2="ffffffff"; //add as many as you need.

const int buttonleft = 6;
const int buttonright = 7; //input digital 8 ta livre

long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

int buttonPushCounterL = 0;   // counter for the number of button presses
int buttonStateL = 0;         // current state of the button
int lastButtonStateL = 0;     // previous state of the button

int buttonPushCounterR = 0;   // counter for the number of button presses
int buttonStateR = 0;         // current state of the button
int lastButtonStateR = 0;     // previous state of the button
/*
 * Initialize.
 */
void setup() {
  digitalWrite(Relay_1, HIGH);
  digitalWrite(Relay_2, HIGH);
  digitalWrite(Relay_3, HIGH);
  pinMode(Relay_1, OUTPUT);
  pinMode(Relay_2, OUTPUT); 
  pinMode(Relay_3, OUTPUT); 
  pinMode(buttonleft, INPUT);
  pinMode(buttonright, INPUT);
  delay(1000); //Check that all relays are inactive at Reset

//      Serial.begin(9600);         // Initialize serial communications with the PC
//    while (!Serial);            // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
    SPI.begin();                // Init SPI bus
    mfrc522.PCD_Init();         // Init MFRC522 card
}


void dump_byte_array(byte *buffer, byte bufferSize) {
    read_rfid="";
    for (byte i = 0; i < bufferSize; i++) {
        read_rfid=read_rfid + String(buffer[i], HEX);
    }
}

void open_lock() {
  digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
}

void pisca_left() {
   digitalWrite(Relay_2, LOW);// set the Relay ON
   delay(350);
   digitalWrite(Relay_2, HIGH);// set the Relay OFF
   delay(350);
}

void pisca_right() {
   digitalWrite(Relay_3, LOW);// set the Relay ON
   delay(1000);
   digitalWrite(Relay_3, HIGH);// set the Relay OFF
   delay(1000);
}

void loop()
{
  buttonStateL = digitalRead(buttonleft);
  int stateRelay = digitalRead(Relay_1);
 
if (stateRelay == HIGH)
    {  // Look for new cards
    if ( ! mfrc522.PICC_IsNewCardPresent())
        return;

    // Select one of the cards
    if ( ! mfrc522.PICC_ReadCardSerial())
        return;

    dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
    Serial.println(read_rfid);
    if (read_rfid==ok_rfid_1 || read_rfid==ok_rfid_2 || read_rfid==ok_rfid_3)
      //ok, open the door.
        open_lock();
      }
  else if
        (buttonStateL != lastButtonStateL) {
    // if the state has changed, increment the counter
    if (buttonStateL == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounterL++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounterL);
     
    } else {
     
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(30);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonStateL = buttonStateL;



  if (buttonPushCounterL % 2 == 0) {
 
    pisca_left();

  } else {
    digitalWrite(Relay_2, HIGH);
  }

 }

     

Go Up