getting arduino's to talk to each other

Just like to say thanks every one for your help so far…what I am trying to do now is to get 3 arduinos to talk to each other and store the data to a sd card so it will not be lost if I lose power ….This code looks for a rfid card’s ( “card 1” or “card2”) then store’s the number of pulses in “count” or “count2” when the count reaches 5 or pin 2 gets 5v+ the count will and the arduino look for another card …. What I would like to do is run this code or something like on two more arduino`s with rfid readers so if a card is found on another arduino it will ask the sd card what is the value of “count” or “count2” and add to it ……. I think the easiest way to do this to store the values on a sd card and read from the sd card every time a card is found or is there a better was ??? my concern is if this can be done what would happen if 2 cards where swiped at the same time on different arduino’s …. I have also included the code for the sd card reader …….i am using arduino megea’s

/*   
 
 pin 2 = -5v in when lost will look for new card
 pin 3 = power
 pin 6 = pulse from switch when 5 pulsese will look for new card
 */


static int lastState = 0;
int count = 0; //to count pulses on card 1
int count2 = 0; //to count pulses on card 2


int powerPin = 3; //to give power
int maxcount = 4; // number of pulses before pin 13 gets power 
int PulsePin =6;  // pulse from switch 



byte data[5];  //For holding the ID we receive
int val = 0; //input from card
byte card1[5] = {
  0x4f,0x00,0x5f,0x40,0x66};
byte card2[5]  = {
  0x4f,0x00,0x5f,0xFe,0x72};

void setup()
{

  pinMode(powerPin,OUTPUT);
  pinMode(PulsePin,INPUT); 


  Serial.begin(19200);// Start serial port 19200 bps (the speed of the rfid reader )

  // Setting Auto Read Mode - EM4102 Decoded Mode - No password
  // command: FF 01 09 87 01 03 02 00 10 20 30 40 37
  Serial.print(0xFF,BYTE);  //Header
  Serial.print(0x01,BYTE);  //Reserved
  Serial.print(0x09,BYTE);  //Length (Command + Data)
  Serial.print(0x87,BYTE);  //Command (0x87 sets auto mode behavior
  Serial.print(0x01,BYTE);  //Data 1: Enable Auto-Read
  Serial.print(0x03,BYTE);  //Data 2: Mode – Parity decoded – Manchester RF/64
  Serial.print(0x02,BYTE);  //Data 3: Total number of block to be read (2)
  Serial.print(0x00,BYTE);  //Data 4: No password expected
  Serial.print(0x10,BYTE);  //Data 5: Password byte 1
  Serial.print(0x20,BYTE);  //Data 6: Password byte 2
  Serial.print(0x30,BYTE);  //Data 7: Password byte 3
  Serial.print(0x40,BYTE);  //Data 8: Password byte 4
  Serial.print(0x37,BYTE);  //Checksum

  delay(500);
  Serial.flush();
  Serial.println();
  Serial.println("RFID module started in Auto Read Mode, Waiting for Card...");
}

void loop()
{
  Serial.println("RFID module started in Auto Read Mode, Waiting for Card...");

  digitalWrite (powerPin, HIGH);

  val = Serial.read();
  while (val != 0xff)
  {  //On Successful read, first byte will always be 0xFF
    val = Serial.read();
    delay(1000);
  }

  //we already read the header (0xff)
  Serial.read();              // reserved
  Serial.read();              // length
  Serial.read();              // command (indicates tag data)
  data[0] = Serial.read();    // we read data 1
  data[1] = Serial.read();    // we read data 2
  data[2] = Serial.read();    // we read data 3
  data[3] = Serial.read();    // we read data 4
  data[4] = Serial.read();    // we read data 5
  Serial.read();              // checksum

  // Indentify RFID Card
  boolean card_1 = true;
  boolean card_2 = true;
  Serial.print(" Card found - Code: ");
  for (int i=0; i<5; i++)
  {
    if (data[i] < 16) Serial.print("0");
    Serial.print(data[i], HEX);

    //cross-check
    if (data[i] != card1[i]) card_1 = false;
    if (data[i] != card2[i]) card_2 = false;
  }
  Serial.println();
  //////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////



  if (card_1) //if card 1 is found  


  {
    Serial.println("Card 1");
    while(!digitalRead(2))//loop until gets 5v on pin 2

    {


      Serial.println(count); 
      if (count > maxcount)     //when count5 gets to the set number sends 5v to output pin
      { 
        Serial.println("pulses gone ");
        return; // returns to start of loop to look for new card 

      } 


      int newState = digitalRead(PulsePin);  //from pulse switch

      if (newState != lastState)     //detect change

      {

        count ++;    //adds one to count



        lastState = newState;

      }
    }

    return;// returns to void loop to look for new card 


  } 









  else if (card_2) //if card 2 is found



  {
    Serial.println("Card 2");
    while(!digitalRead(2))

    {


      Serial.println(count2); 
      if (count > maxcount)     //when count5 gets to the set number sends 5v to output pin
      { 
        Serial.println("pulses gone ");
        return; // returns to start of loop to look for new card 

      } 


      int newState = digitalRead(PulsePin);  //from pulse switch

      if (newState != lastState)     //detect change

      {

        count2 ++;    //adds one to count



        lastState = newState;

      }
    }

    return;// returns to start of loop to look for new card 


  } 




  else Serial.println("Not Recognized! ");//if card is not registered
  Serial.println();
}
/*
 * SD_uFAT Basic - Basic use of SD Cards
 *
 * Copyright (C) 2008 Libelium Comunicaciones Distribuidas S.L.
 * http://www.libelium.com
 *
 * This example allows manipulating one file stored in the SD card called "hola.txt"
 * I have chosen "hola", meaning "hello" in Spanish, just because I was a little 
 * tired of the classic "hello world". The file should be stored in the card and comes
 * in the ZIP file containing this example. You manipulate the file by simple commands
 * over the serial port. Here a list:
 *
 * - H: prints a help message with all commands implemented in the example
 * - L: lists the file's info, calls the ls("hola.txt") function
 * - D: deletes the file, calls del("hola.txt")
 * - P: prints a string to the file, in this case "hola caracola"
 * - W: allows you intialize the file entering text by hand through the serial terminal
 *      (beware, it erases the file)
 * - R: dumps the contents of "hola.txt" to the serial port
 * - A: append data interactively to the end of the file
 *
 * BUT HOW DOES IT WORK?
 * I use the SD uFAT approach, this example offers a series of functions  
 * to manipulate the content of SD cards. The only premise is that the
 * files in the card must be pre-existing. A good way is to add text
 * documents created with gedit (LIN), textedit (MAC) or notepad (WIN)
 *
 * The documents could be filled up with any characters of your choice
 * and should have a certain size. The uFAT approach to controlling SD
 * cards won't allow listing directories, or modifying the file size. I
 * fill up my files with blank spaces (0x20), it makes easier to look at
 * the files in a text editor later
 *
 * The end of files will be marked with the character End Of Text, we choose
 * it to be 0x03 as in ASCII. This will be the programatical trick to look
 * for the end of file. Just to make things easier for you, it is possible
 * to download some pre-made empty files in different sizes with the 0x03
 * character in first position from http://blushingboy.net, also there are some
 * coming in the ZIP where you got this example from
 *
 * The functions implemented for this library can be categorized in basic,
 * advanced, and experimental (unstable); they are:
 *
 * BASIC
 * - ls(filename): lists the size, amount of sectors, and real use of a file
 * - del(filename): erases a file by putting a NULL character at the beginning 
 *                  of all its sectors
 * - print(filename, string): appends a string at the end of a file [1]
 * - println(filename, string): appends a string + EOL at the end of a file
 * - cat(filename): prints out the contents of a file to the serial port
 * - append(filename): will listen to the serial port and append the data to the file
 *
 * ADVANCED
 * - usedBytes(filename): answers how many bytes are actually in use in the card
 * - startSector(filename): answers the sector on the SD card where a file starts
 * - verbose(mode): mode == ON (default) will print out help strings to the port
 *                  mode == OFF will get the functions to work in silent mode
 *
 * EXPERIMENTAL (unstable or not implemented)
 * - append(filename1, filename2): appends filename2 at the end of filename1
 * - indexOf(filename, string): looks for a string in a file answering the position
 * - indexOfLine(filename, int): gets the offset in the file to the line determined by the parameter
 *
 * HOW TO USE THIS PROGRAM
 * The way to interact with this example is through a serial monitor. Arduino's is good
 * but any others will also work. I recommend the following: GtkTerm (LIN), ZTerm (MAC),
 * and Brayterminal (WIN). All are free or freeware and can be obtained from different
 * sources
 *
 * MAKING YOUR OWN CODE OUT OF THIS ONE
 * To make this example I have been streching the variable space to the limit, you can
 * easily implement programs reading analog sensors or buttons and store that data
 * as in the SD card. You can probably read information from the serial port
 * or sensors hanging on I2C and push it into the card. If you ran into problems, just
 * remember the issue about the variable space. You cannot reduce the buffer[512] variable,
 * but you could work out with DATABUFFERSIZE (defaulting to 32) to get some extra room
 *
 * HACKING THE LIBRARY
 * The code compiles to a small sized library (7KB), however, it can be even smaller if you
 * you just erase the functions you don't need from the library, customizing it to fit
 * your needs. Also, even if the buffer[512] cannot be changed in size, you can use it for
 * other things during the time it is not used to access the SD card. In this way it could be
 * e.g. the temporary memory for a graphical display, swap for some of your processes, etc
 *

 */

#include "SDuFAT.h"

// define the pin that powers up the SD card
#define MEM_PW 8

// help string to be sent to the serial port
#define HELP "H help\nL file info\nD delete\nP append string\nW init file and write\nR dump to serial\nA append text\n"

// variable used when reading from serial
byte inSerByte = 0;

void setup(void)
{
  // on my MicroSD Module the power comes from a digital pin
  // I activate it at all times
  pinMode(MEM_PW, OUTPUT);
  digitalWrite(MEM_PW, HIGH);
  
  // configure the serial port to command the card and read data
  Serial.begin(19200);
}

void loop(void)
{
  // Arduino expects one of a series of one-byte commands
  // you can get some help by sending an 'H' over the serial port
  if (Serial.available() > 0) {
    int result = 0;
    inSerByte = Serial.read();
    switch (inSerByte) {
    case 'H':
      Serial.println(HELP);
      result = 3; // special output for help message
      break;
    case 'L':
      result = SD.ls("hola.txt");
      break;
    case 'R':
      result = SD.cat("hola.txt");
      break;
    case 'W':
      result = SD.write("hola.txt");
      break;
    case 'A':
      result = SD.append("hola.txt");
      break;
    case 'P':
      result = SD.println("hola.txt","\nhola caracola");
      break;
    case 'D':
      result = SD.del("hola.txt");
      break;
    default:
      result = 2; // value for unknown operation
      break;
    }
    
    // print a status message for the last issued command
    // for help (result == 3) won't print anything
    if (result == 1) SD.printEvent(ERROR, "hola.txt");
    else if (result == 2) SD.printEvent(WARNING, "unknown command");
    else if (result == 0) SD.printEvent(SUCCESS, "hola.txt");
  }
}

I am having trouble making sense of this part:

when the count reaches 5 or pin 2 gets 5v+ the count will and the arduino look for another card

Do you want there to be a single central counter somewhere that is updated when any of the Arduinos detect the corresponding RFID? Or do you want each Arduino to maintain its own copy of the counters? Or something else?

Are you building a de-centralized system, where each Arduino has its own SD, and all operations are replicated to all other Arduinos for backup? Or are you building a star topology system where one Arduino has the SD card reader/writer, and the others just talk to that? The answer to your question varies based on this. Btw: your question is kind-of loosely framed -- do you need design help? software help? wiring help? part selection help?

Regarding the "what if two cards are swiped at the same time," that's what protocol design is for. Any good multi-point protocol will arbitrate resources and let one "win" over the others. Typically, the others will then "back off" a bit and try again later.

Suppose this is what you want to build: 1. There's one node that contains a list of accepted cards and the number of times used. 2. There's between 1 and N nodes that are hooked to a card reader and some kind of output. 3. Each time a card is swiped on a card reader, the node sends the card code to the first node, which either replies back and says "card not OK/found," or replies back and says "card OK, used X times" (after which it updates the SD card with the new use count)

I would look into the Ethernet Shield, which will let you run robust communications between the different nodes. You can establish a TCP channel to the "master" node each time you want to check a card, and the master node can accept and process one connection at a time.

I think I remember this project! I think the last time you asked, I suggested getting the Ethernet shields, and making the central station be a cheap computer of some sort (such as a $199 netbook).

I still recommend Ethernet shields! They let the nodes talk to the central station right away. The SD card has to be hand-carried to be read by some other station. If you buy a card worth 1 pints, then if your synchronization mechanism is "carry SD card" then someone can draft 1 pint per station, and the stations wouldn't know that the others already have dispensed a pint too.

With Ethernet and a central computer, there is no risk of double-spending.

Yes, you would need ethernet capability at each station, wired or wireless and a router to let them talk together. The ethernet library looks as though it would make this fairly easy. For the central station, you could perhaps use an Arduino Ethernet board as it also has an SD card reader. Then you can store the RFIDs and number of credits on the card (and in RAM) and reload them on power up. Then you would need some interface (buttons & LCD) to allow you to add credits at that station. The satellite stations would then just have to ask the master “How many credits does RFID xyz have” to decide whether to dispense lager and let the master know after they’ve poured it.

thanks wildbill ...... so i need another 3 Ethernet Shield V2 thats 4 in total ....and 4 arduino's in total and some buttons and an lcd is that correct

Yes, you need one shield per Arduino station. You don't need a router; just a "hub" or "switch" (can probably be had for $10-$20 from Amazon) If you're not a software engineer by trade or hobby, yes, you probably have a big project ahead of you. Even for trained software engineers, networking (and the distributed nature thereof) is considered pretty hard.

So back to my original question can anyone give me a price to write this code please

Post a complete description of what you want done in Gigs and Collaborations. If you provide the hardware to build and test the code on, I'd be interested.

Post a complete description of what you want done

There's the problem - it can be very very difficult to do, particularly if you haven't written specifications before. Depending on how much you're loving or hating your Arduino experience, it might be worth playing around a bit more to try and get a pre-prototype working with a base station and satellite. It'll help you figure out what the requirements are.

Alternatively, just be prepared for a bit (or a lot) of back & forth when you discover that what you asked for isn't actually exactly what you want.

Sounds like a fun project; one more thing though, make sure you know what PaulS looks like so you can frisk him for 'free beer' RFID tokens if he ever comes to Barnsley ;)

one more thing though, make sure you know what PaulS looks like so you can frisk him for 'free beer' RFID tokens if he ever comes to Barnsley

Hey, I'd send all the tokens back. The ones I could find, anyway...

maddave: So what your saying is unless i can get some one to write the code ..... Don’t waste my money …..So back to my original question can anyone give me a price to write this code please ?????????

Depends on two things: 1) How advanced you want the system 2) How hard and long you try to learn and keep at it

If the system is a single system, with an SD card, a solenoid, a flow counter, and an RFID reader, then you can probably do this using available Arduino resources on this site and Google search.

As soon as you want a multi-node system, with controls against double-spending, you enter a totally different world of distributed systems. At that point, you need not only the Arduino, but the back-end, and networking, and protocol design, added to the mix. If you've never done commercial project development before, prepare for another kind of learning curve!

A quote for writing the code would depend on what you already have, and specifically what you want done. I'm personally imagining something like:

  • Dispenser node, using an Ethernet shield, a solenoid controller, a couple of LEDs and/or a 16x2 character LCD, a flow pulse counter, and a RFID reader.
  • Networking using a simple Ethernet switch and Ethernet Cat5 cabling.
  • A controller system using a low-cost computer running some modern version of Linux (like Ubuntu netbook edition)
  • A RFID reader node for the computer, to allow a cashier to scan a tag when adding value. This does serial/USB communication and RFID reading only.
  • Some simple management of reporting dispensers running low/dry/trouble.
  • A web browser based interface running on the low-cost computer, that allows a cashier to scan a RFID tag, check its stored value, and add value to it through a simple form.
  • When a user scans a tag at a dispenser, the dispenser checks with the master node, using the Ethernet shield. If there is sufficient funds, funds get subtraceted on the master node, and the dispenser is run. Else the user is showed an error (either red LED, or LCD character display)
  • A report that shows value added and spent per RFID tag, and value dispensed per dispenser station.
  • Management to "clear out" the database and start over.

Backup and financial accounting/reporting would have to be done outside of this system, if needed.

Assuming you (or someone else) could ruggedize this, prepare it for deployment, get whatever food dispensing certifications are needed, and do all the other "hardware" and "commerce" parts, then I think this would bring a system to a point where a "demo" could be run after training a few cashiers in how to add value to the chips, and having someone around to fix the bar machines when they physically break/clog/run dry.

To develop this software, anyone doing contracting needs at least "one of each" of those pieces of hardware; ideally two of the dispenser nodes just to verify multi-user usage. (Note: the actual flow counter or solenoid controller hardware may not be needed, depending on what the hardware interface looks like, and/or whether controller functions already exist for those)

I'd probably bid about $1,500 for the webapp software and Linux configuration to build a "master node" (probably in something simple like Python/Twisted or Node.js, with a web page/javascript front-end), and about $4,500 for the Ardy dispenser node software. I'd also want a 1% of gross sales royalty. In Silicon Valley, where I live and work, this bid translates to roughly one to two weeks of contract development, although this particular project I'd take on "fixed fee to deliver requirements" and "time as available" basis. I'm much less experienced in the specifics of Arduino integration with the devices you describe, than I am in systems engineering/architecture and distributed software development.

I think it would be a super fun project! But this is not an official bid, because I'm not even sure I could clear this with my current employer, and IIRC, you're in Europe, which adds some time zone and distance development complexities. However, I hope that this way of starting to break down requirements and steps to build a working system (as opposed to just a working device) helps you move forward on your self-service bar idea!

$4,500 for the Ardy dispenser node software. I'd also want a 1% of gross sales royalty

what world do you live in ???????????

thanks but i have it sorted