Tuesday, November 11, 2014

How to Interrupt Your Arduino


In this video tutorial we will look at how to use interupts on the Arduino Uno, Arduino Pro Mini, and any Arduino using the Atmega 328. Interrupts allow you to react immediately to an external event versus trying to constantly check for it in the main loop. 



Arduino Code****************************************************************
/*
  Interrupt Example
  Demonstrates how to use interrupts. When a high level is detected at pin 2 interrupt occurs and blinks LED at pin 13

  This example code is in the public domain.
 */

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
//create a variable for ISR, has to be volatile if used in interrupt
volatile int bLED = 0;

void setup() {
  //Create interrupt: 0 for pin 2 or 1 for pin 3, the name of the interrupt function or ISR, and condition to trigger interrupt 
  attachInterrupt(0, interruptFunction, HIGH); 
  pinMode(led, OUTPUT); //set up the LED pin to output
}

// the loop routine runs over and over again forever:
void loop() {
  
  if(bLED) { //if this statement becomes true then the interrupt occurred
    digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(300);               // wait 
    digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
    delay(300);               // wait 
  }
  /*
  noInterrupts(); //disables interrupts
  // critical, time-sensitive code here
  interrupts();//enables interrupts
  */
}

//This is the function called when the interrupt occurs (pin 2 goes high)
//this is often referred to as the interrupt service routine or ISR
//This cannot take any input arguments or return anything
void interruptFunction() {
 bLED = 1; //with this variable set to 1 the LED will blink
 //detachInterrupt(0); this function call will turn the interrupt off
}

Wednesday, October 29, 2014

Building an Android App to Communicate with the RN-42 Bluetooth Module

In this video we will build an Android App to communicate with the RN-42 Bluetooth module. The RN-42 is connected to Arduino Uno and the Android App we build turns on and off an LED connected to the Arduino. Below the video you will find the Arduino code and a link to download the Android App code (MIT Inventor 2 was used to build the Android App). Enjoy!



Link to download App Inventor 2 code (.aia file):
https://dl.dropboxusercontent.com/u/26591541/AndroidBTExample.aia

Arduino Code:
/*
  This sketch is part of a tutorial for connecting to and communicating with an HC-06 or an RN-42 bluetooth module using a custom Android App. 
  The bluetooth modules are connected to an Arduino and the Arduino is connected to an LED. The Android app is used to wirelessly turn on and
  off the LED using  bluetooth. 

  This code is in the public domain.
 */

// Pin 7 has a LED connected to it
int led = 7;

// the setup routine runs once when you press reset:
void setup() {
  
  Serial.begin(9600);
  // initialize the digital pin as an output and set it low initially
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
}

// the loop routine runs over and over again forever:
void loop() {
  delay(30);
  String t; //create an empty string to store messages from Android
  while(Serial.available()) { //keep reading bytes while they are still more in the buffer
    t += (char)Serial.read(); //read byte, convert to char, and append it to string
  }
  
  if(t.length()) { //if string is not empty do the following
    if(t == "on") { //if the string is equal to "on" then turn LED on
      digitalWrite(led, HIGH); //Set digital pin to high to turn LED on
      Serial.write("LED is on"); //Tell the Android app that the LED was turned on
    }
    else if (t == "off") { 
      digitalWrite(led, LOW);  
      Serial.write("LED is off");
    } // turn the LED off by making the voltage LOW
  }
}

Friday, October 24, 2014

Building an Android App to Communicate with the HC-06 Bluetooth Module

In this video we will build an Android App to communicate with the low cost HC-06 Bluetooth module. The HC-06 is connected to Arduino Uno and the Android App we build turns on and off an LED connected to the Arduino. Below the video you will find the Arduino code and a link to download the Android App code (MIT Inventor 2 was used to build the Android App). Enjoy!



Link to download App Inventor 2 code (.aia file):
https://dl.dropboxusercontent.com/u/26591541/AndroidBTExample.aia

Arduino Code:
/*
  This sketch is part of a tutorial for connecting to and communicating with an HC-06 or an RN-42 bluetooth module using a custom Android App. 
  The bluetooth modules are connected to an Arduino and the Arduino is connected to an LED. The Android app is used to wirelessly turn on and
  off the LED using  bluetooth. 

  This code is in the public domain.
 */

// Pin 7 has a LED connected to it
int led = 7;

// the setup routine runs once when you press reset:
void setup() {
  
  Serial.begin(9600);
  // initialize the digital pin as an output and set it low initially
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
}

// the loop routine runs over and over again forever:
void loop() {
  delay(30);
  String t; //create an empty string to store messages from Android
  while(Serial.available()) { //keep reading bytes while they are still more in the buffer
    t += (char)Serial.read(); //read byte, convert to char, and append it to string
  }
  
  if(t.length()) { //if string is not empty do the following
    if(t == "on") { //if the string is equal to "on" then turn LED on
      digitalWrite(led, HIGH); //Set digital pin to high to turn LED on
      Serial.write("LED is on"); //Tell the Android app that the LED was turned on
    }
    else if (t == "off") { 
      digitalWrite(led, LOW);  
      Serial.write("LED is off");
    } // turn the LED off by making the voltage LOW
  }
}


Saturday, October 18, 2014

Building a Smart Thermostat Part 3

Welcome to the third and final part of the Smart Thermostat project! In part three we build the Android App to monitor and control the thermostat remotely via Bluetooth. To build the Android App we will use MIT App Inventor 2. If you would like a copy of the Android App code just email me at forcetronics@gmail.com



To get Arduino code from GitHub: https://github.com/ForceTronics/SmartThermoStat

To access MIT App Inventor 2 go to: http://ai2.appinventor.mit.edu\


Sunday, August 31, 2014

Building a Smart Thermostat Part 2

This is part 2 of the Smart Thermostat project. In part 2 we add the following features to our thermostat design:

  • Bluetooth control so the thermostat can be controlled remotely so you can control the temperature of your home from the comfort of your couch or bed
  • Mount the project so it is in a much more usable and aesthetically pleasing form then the prototype form it we saw in part 1
  • A power save mode to cut down on the utility costs 

In part three we will create the Android app and add a power supply to run it off of the 24 VAC signal coming from the HVAC system. To download the Arduino code follow the GitHub link below. Please share your comments!

Arduino code from GitHub


Smart Thermostat Part 2

Sunday, August 10, 2014

Getting Started with the HC-06 Bluetooth Module

In this video we look at how to get started with the HC-06 Bluetooth transceiver module. The HC-06 is a great low cost way to add wireless communication to any project. Since the HC-06 uses a serial line to communicate it is easy to pair it with an Arduino.


AT CommandREply from HC-06COMMENTs
ATOKUsed to verify communication
AT+VERSIONOKlinvorV1.8The firmware version
AT+NAMEmyBTOKsetnameSets the module name to “myBT”
AT+PIN1234OKsetPINSets the module PIN to 1234
AT+BAUD1OK1200Sets the baud rate to 1200
AT+BAUD2OK2400Sets the baud rate to 2400
AT+BAUD3OK4800Sets the baud rate to 4800
AT+BAUD4OK9600Sets the baud rate to 9600
AT+BAUD5OK19200Sets the baud rate to 19200
AT+BAUD6OK38400Sets the baud rate to 38400
AT+BAUD7OK57600Sets the baud rate to 57600
AT+BAUD8OK115200Sets the baud rate to 115200
AT+BAUD9OK230400Sets the baud rate to 230400
AT+BAUDAOK460800Sets the baud rate to 460800
AT+BAUDBOK921600Sets the baud rate to 921600
AT+BAUDCOK1382400Sets the baud rate to 1382400

Method 1 Setup
/*This sketch Configures the name and baud rate of an HC 06 Bluetooth module */
char message1[10];//need length of chars being read +1 for null character
char message2[9];

void setup() {
  // set baud rate then delay to give user time to open serial monitor
  Serial.begin(9600);
  delay(5000);
  //Send command to set name of HC06 module, with the below command name will change to "forcetronics"
  Serial.print("AT+NAMEForceT");
  delay(600); //HC06 requires 500 msec for reply
  int8_t count = 0; //declare and intialize count 
  while(1) { //loop until OKsetname is read and cleared from buffer
    if(Serial.available()) {
        message1[count] = Serial.read(); //read in char
        count++; 
        if(count == 9) break; //after we get all 9 char break out of loop
    }
    delay(10);
  }
  
  //Send AT command to change baud rate to 115200
  Serial.print("AT+BAUD8");
  delay(600); //HC06 requires 500 msec for reply
  count = 0; //intialize count
  while(1) { //loop until OK115200 is read and cleared from buffer
    if(Serial.available()) {
        message2[count] = Serial.read(); 
        count++; 
        if(count == 8) break; 
    }
    delay(10);
  }
  
  //print out each message to make sure it worked
  Serial.println("");
  Serial.println(message1);
  Serial.println(message2);
}

void loop() {
 //do nothing
  delay(50);
}


Method 2 Setup


//Example code for testing a serial bluetooth device using Arduino and a serial terminal on a computer
void setup() {
  // set baud rate to match BT module
  Serial.begin(115200);
}

void loop() {
  
  String t; //string to hold data from BT module 
  while(Serial.available()) { //keep reading bytes while they are still more in the buffer
    t += (char)Serial.read(); //read byte, convert to char, and append it to string
  }
  
  if(t.length()) { //if string is not empty do the following
    
    if(t == "Hi Uno\r\n") { Serial.print("Hello Neil\n"); } //say hello
    else if(t == "Meaning of life?\r\n") { //find out the meaning of life
      delay(1000);
      Serial.print("Money. ");
      delay(1000);
      Serial.print("Guns. ");
      delay(1000);
      Serial.print("Hoes.\n");
      delay(1000);
      Serial.print("Arduino.\n");
   }
   else { Serial.print("Syntax Error\n"); } //send this for any other string
  }
   delay(20);
}

Friday, July 25, 2014

Building a Smart Thermostat Part 1

In this project we will build a smart thermostat with features such as high accuracy temperature measurements, customized user interface, and, best of all, wireless control from an Android device. Since this is a big project it will be presented in three parts. This is part one where we will cover the basic design and get to a working prototype. To download the Arduino code follow the GitHub link below. Please share your comments!

Arduino code from GitHub



Smart Thermstat Part 1 (prototype)