Friday, July 19, 2013

NMEA checksum calculator



Understanding NMEA Checksum

 
The checksum (CS) is generated by X-OR’ing all the data characters together in sequence, starting with the first ‘data’ character and XOR it with the next one. Then take that result and XOR it with the next, and so on.
Note: The ‘$’ and ‘*’ characters are excluded from calculation.
The example below show how to calculate the CS for the word “test”.


Note: You can use the windows or ubuntu calculator in advance mode to perform this operation.
Step1:
t XOR e


 
Result1 = 0010001 = 11 HEX

Step2:
Result1 XOR s

Result2 = 1100010 = 62 HEX
 
Step3:
Result2 XOR t


 
CS = 0010110 = 16 HEX
 External links

Enough talking, now it's fun time!

Arduino code

// Sample sentences to XOR
//$test*16
//$GPRMC,023405.00,A,1827.23072,N,06958.07877,W,1.631,33.83,230613,,,A*42

const byte buff_size = 80; // buffer size must be a constant variable
char buffer[buff_size];
byte index = 0;   // declare all variables that will hold numbers less than '255' as 'byte' data type, because they require only '1-byte' of memory ('int' uses 2-bytes).
byte start_with = 0;
byte end_with = 0;
byte CRC = 0;
boolean data_end = false; // Here we will keep track of EOT (End Of Transmission).

void setup(){

  Serial.begin(9600);
  Serial.println("Serial communication initialized"); // Print to serial port so we know its working.

}


void loop(){

  while (Serial.available() > 0){
    char inchar = Serial.read();
    buffer[index]  = inchar;

    if( inchar == '$'){
      start_with = index;
    }

    if(inchar == '*'){
      end_with = index;
    }

    index++;

    if(inchar == '\n' || inchar == '\r'){ // if 'new line' or 'carriage return' is received then EOT.
      index = 0;
      data_end = true;
    }
  } 


  if (data_end == true){
    for (byte x = start_with+1; x<end_with; x++){ // XOR every character in between '$' and '*'
      CRC = CRC ^ buffer[x] ;
    }
  }

  if(CRC > 0){
    Serial.println(CRC,HEX); // print calculated CS in HEX format.
    CRC = 0; // reset CRC variable
    data_end = false; // Reset EOF so we can process more incoming data.
  }


}

//Elimeléc López - July-19th-2013







Friday, June 14, 2013

Measure RPMs Arduino



Video




Schematic





The Fan



Code

Download RPM.ino
// read RPM

volatile int rpmcount = 0;//see http://arduino.cc/en/Reference/Volatile
int rpm = 0;
unsigned long lastmillis = 0;

void setup(){
 Serial.begin(9600); 
 attachInterrupt(0, rpm_fan, FALLING);//interrupt cero (0) is on pin two(2).
}

void loop(){
 
 if (millis() - lastmillis == 1000){  /*Uptade every one second, this will be equal to reading frecuency (Hz).*/
 
 detachInterrupt(0);    //Disable interrupt when calculating
 
 
 rpm = rpmcount * 60;  /* Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/
 
 Serial.print("RPM =\t"); //print the word "RPM" and tab.
 Serial.print(rpm); // print the rpm value.
 Serial.print("\t Hz=\t"); //print the word "Hz".
 Serial.println(rpmcount); /*print revolutions per second or Hz. And print new line or enter.*/
 
 rpmcount = 0; // Restart the RPM counter
 lastmillis = millis(); // Uptade lasmillis
 attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
  }
}


void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
  rpmcount++;
}


// Elimelec Lopez - April 25th 2013

Code v2 (Calculate Average)
Download RPM_V2.ino

// read RPM and calculate average every then readings.
const int numreadings = 10;
int readings[numreadings];
unsigned long average = 0;
int index = 0;
unsigned long total; 

volatile int rpmcount = 0;//see http://arduino.cc/en/Reference/Volatile 
unsigned long rpm = 0;
unsigned long lastmillis = 0;

void setup(){
 Serial.begin(9600); 
 attachInterrupt(0, rpm_fan, FALLING);
}

void loop(){
 
  
 if (millis() - lastmillis >= 1000){  /*Uptade every one second, this will be equal to reading frecuency (Hz).*/
 
 detachInterrupt(0);    //Disable interrupt when calculating
 total = 0;  
 readings[index] = rpmcount * 60;  /* Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/
 
 for (int x=0; x<=9; x++){
   total = total + readings[x];
 }
 
 average = total / numreadings;
 rpm = average;
 
 rpmcount = 0; // Restart the RPM counter
 index++;
 if(index >= numreadings){
  index=0; 
 } 
 
 
if (millis() > 11000){  // wait for RPMs average to get stable

 Serial.print(" RPM = ");
 Serial.println(rpm);
}
 
 lastmillis = millis(); // Uptade lasmillis
  attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
  }
}


void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
  rpmcount++;
}








Saturday, June 8, 2013

Door Open with Dallas iButton Arduino

 
 Requirements
 
 
 Video

 
Schematic 

 
 
 
 
 Breadboard Connection

 


 Code

 
 #include <OneWire.h>

OneWire ibutton (2); // I button connected on PIN 2.

byte ibuttonid[10] = {1,144,74,165,21,0,0,143}; // Ibutton ID must be typed in Decimal in the following order: Family code (01 for DS1990); numbers from R. to L.; CRC. 
byte buffer[10]; //array to store the readed Ibutton ID.

boolean result;  // this variable will hold the compare result

int doorpin = 13; // the output pin to activate the door.

void setup(){
    Serial.begin(9600); 
    pinMode(doorpin,OUTPUT);
   
}

void loop(){
   
 if (!ibutton.search (buffer)){//read attached ibutton and asign value to buffer
    ibutton.reset_search();
    delay(200);
    return;
 }
  
  for (int x = 0; x<8; x++){  
    Serial.print(buffer[x],HEX); //print the buffer content in LSB. For MSB: for (int x = 8; x>0; x--) 
    Serial.print(" "); // print a space
   }
   Serial.println("\n"); // print new line
   
   // compare the ibutton id
   
  result = true; // set variable equal to one.
  for (int x=0; x<10; x++){ 
  int compare1 = ibuttonid[x];// asign each index of arrays to test, one by one and compare
  int compare2 = buffer[x];
  
  
  if(compare1 != compare2){ // if any index comparison is not equal, then the arrays are not equal and result will be 0.
   result = false; 
  }
  
  }
  if(result == true){ // if the arrays are equal, do something.
   Serial.println("Door open for 5 seconds."); 
   digitalWrite(doorpin,HIGH); // Turn on LED on pin 13 (build-in on the Arduino board)
   delay(5000); // wait five seconds. (1 second = 1000 milliseconds)
   Serial.println("Door closed");
   digitalWrite(doorpin,LOW); // turn off LED.
 }
   
    //set buffer back to cero.
    
    for (int x=0; x<10; x++){
     buffer[x] = 0 ;
    }                
}



/* by Elimeléc 
  Jun-07-2013
*/

Friday, June 7, 2013

Read Dallas iButton Arduino

Requirements:
 
-Arduino board
-OneWire Library (unzip and copy to arduino-1.0.3/libraries)
-Maxim Ibutton DS1990
-Maxim Touch Probe Read
 
 
 Schematic
 
 
 Breadboard Connection

 
Code: 
  
 #include <OneWire.h>

OneWire ibutton (2); // I button connected on PIN 2.

byte buffer[20]; //array to store the Ibutton ID.

void setup(){
 Serial.begin(9600); 
   
}

void loop(){
   
 if (!ibutton.search (buffer)){//read attached ibutton and asign value to buffer
    ibutton.reset_search();
    delay(200);
    return;
 }
  
  for (int x = 0; x<8; x++){  
    Serial.print(buffer[x],HEX); //print the buffer content in LSB. For MSB: for (int x = 8; x>0; x--) 
     Serial.print(" "); // print a space
   }
   Serial.println("\n"); // print new line
   
   //crc compute//
   byte crc;
   crc = ibutton.crc8(buffer, 7);
   Serial.println(crc,HEX);
 
     
} 
 
/*by Elimeléc
 Jun-07-2013
*/ 
 

Compare arrays in Arduino

Sample Code:


// Compare arrays

int array1[6] = {0,1,2,3,4,5}; // sample arrays
int array2[6] = {0,1,2,3,4,5};
boolean result;  // this variable will hold the compare result

void setup(){
     Serial.begin(9600); // stablish serial communication
  }
  
  
  
 void loop(){
  
  result = true; // set variable equal to one.
  for (int x=0; x<6; x++){ 
  int test1 = array1[x];// asign each index of arrays to test, one by one and compare
  int test2 = array2[x];
  
  
  if(test1 != test2){ // if any index comparison is not equal, then the arrays are not equal and result will be 0.
   result = false; 
  }
  
  }
  if(result == true){ // if the arrays are equal, do something.
  Serial.println(result); 
  }
  delay(100);
 }