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);
 }