Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Convert unsigned char (hex) to long on: September 23, 2011, 12:36:14 pm
I finally got my RFID code working. The output of the card ID is: 00A4EA59. When I convert this from HEX to DEC (in calculator) I get 10807897 which is the code of my card.
But how can I do this conversion in the Arduino code ?
I tried strtol but this gave the obvious error that a const char is expected instead of an unsigned char.
Anybody got an idea ?

The complete code:
Code:
unsigned char card[12];                    // Contains the value of the RFID card
unsigned char prevcard[12];                // Contains the value of the previously read card
unsigned char i=0;                         // number of read loops
unsigned int different=0;                  // Check if the card has been read already
unsigned long prevmillis=0;                // Contains time of last use card
unsigned long millisdelay=5000;            // Timeout between use of same card

void setup() {
  Serial.begin(9600);                      // Start Serial interface to computer
  Serial1.begin(9600);                     // Start Serial interface for RFID reader
  Serial.println("Start Cardreader");
  Serial.println("----------------------------------");
}

void loop() {
  if (Serial1.available()>0) {            // Check if a card has been read
    card[i]=Serial1.read();               // Fill the array with the value, 10 times needed
    if ((millis()-prevmillis)>millisdelay || millis()<prevmillis) {    // Check if the delay has been crossed or that millis started from 0 again
      memset(prevcard,0,12);              // Clear the prevcard array
    }
    i++;                                  // add 1 to i
    if (i>=11) {                          // Check if loop has passed 11 times
      i=0;                                // Reset i
      if (memcmp(prevcard,card,12)!=0) {  // Check if the previous read card is different from the current
        different=1;              
      }
      if (different) {                    // If previous card is different
        memmove(prevcard,card,12);        // Copy card array contents to prevcard array
        different=0;                      // Reset different check
        prevmillis=millis();              // Set new last read time
        decode();                         // Do something with the RFID keycode
        Serial.println("----------------------------------");
      }      
      delay(10);                          // Delay to avoid double reading
      Serial1.flush();                    // Clear the serial buffer so we can read a new card
    }
  }
}

void decode() {
  Serial.print("Checksum: ");
  for(int p=0;p<4;p++) {                // Write only the checksum, not the card number
    Serial.print(card[p]);
  }
  Serial.print(", Card ID: ");
  for(int p=3;p<11;p++) {                // Write only the card number, not the checksum, outputs 00A4EA59
    Serial.print(card[p]);
  }
  Serial.println("");
}

EDIT: I'm using the Arduino Mega, that's why there's a Serial1
2  Topics / Home Automation and Networked Objects / Re: Serial communication from browser on: June 30, 2011, 03:09:10 am
That's actually a nice idea smiley
And true, the coolness factor increases 10-fold.

I currently have the following code:
Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int ledPin13 = 13;                 // LED connected to digital pin 13
int incomingByte;      // a variable to read incoming serial data into
char* LCDStrings[]={"Incoming chat","Click stop flash","Alarm stopped","Have a good chat"};

void setup() {
  lcd.begin(16, 2);
  lcd.clear();
  pinMode(ledPin13, OUTPUT);      // sets the digital pin as output
  Serial.begin(9600); // initialize serial communication
}

void loop() {
 if (Serial.available() > 0) {
  incomingByte = Serial.read(); // read the oldest byte in the serial buffer

  while (incomingByte == 48) {
    PrintLCD(0);
    digitalWrite(ledPin13, HIGH);   // sets the LED on
    delay(250);
    digitalWrite(ledPin13, LOW);   // sets the LED on
    if (Serial.available() > 0) {
      incomingByte = Serial.read(); // read the oldest byte in the serial buffer
    }
    delay(250);
  }
  if (incomingByte == 177) {
    digitalWrite(ledPin13, LOW);    // sets the LED off
    PrintLCD(2);
    delay(3000);
    lcd.clear();
  }
 }
}

int PrintLCD(byte messageid) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(LCDStrings[messageid]);
  lcd.setCursor(0, 1);
  lcd.print(LCDStrings[messageid+1]); 
}

Is this ok ? Are there optimalizations possible ?
3  Topics / Home Automation and Networked Objects / Re: Serial communication from browser on: June 29, 2011, 12:41:27 pm
Thanks for the tip !
Googling for serial and php gave tons of pages, but finally found something that works smiley
I used serialcomm.zip from http://missionduke.com/arduino-projects/

Leds flickering with a status update on the LCD: check, next step: connecting it to my relay.
4  Topics / Home Automation and Networked Objects / Re: Serial communication from browser on: June 29, 2011, 08:14:59 am
Attached is the schema of what I'm trying to do.
5  Topics / Home Automation and Networked Objects / Serial communication from browser on: June 29, 2011, 08:02:56 am
Hi everybody,

I'm trying to do the following:
I'm using a live chat application. The problem is that I'm not always looking at my monitor. To make sure I don't miss any chats I wanted to activate a flashing lamp when a chat opens.

What I've got now:
Program that talkes with Arduino through serial USB (prossessing on windows Vista), lamp connected to the Arduino through a relay. Chat window in which I can change the code (or implement an iframe if necessary).

What I still need:
A way to send serial commands (ethernet shield isn't an option) from the browser to the Arduino. It's only for 1 PC, so if I have to install software locally, that's possible.
Anybody that can point me in the right direction ?
Pages: [1]