// Project#13 - Arduino UNO // In this project we are going to hook up an ordinary Arudino UNO to our PC and have it control either 4x LEDs or a 4-channel relay board. // We'll be creating A GPI based API that toggles the states of each relay, or turn them all off. // For this project you'll need a 4-channel relay board, or 4x LEDs with 4x current limiting resistors. // You can use the RPI_no_RPI for this project if you'd like, but the idea is to use an off-the-shelf standard UNO // Connections: // Connect 5v from the UNO to the 5v supply pin of your 4-channel relay board. // Connect GND from your UNO to the GND (ground) of your relay board. // Connect Arduino GPIOs 2 through 5 to the rleay drive pins on the relay board // If you're using LEDs, connect the anodes (positive) of each LED to GPIOS 2 through 5, and the cathodes (negative) to current-limiting... // ... resistors in the area of 300-1000 ohms. Connect the second side of the reistors to GND (ground). That way, when the relay GPIOS... // ... Turn on, the LEDs turn off, and when the relays turn off, the LEDs turn on. The logic is backwards because the relays for my... // ... relay board were active-low, which means that when the drive pins are pulled to 0v, the relay turns on, and when set to 5v... // ... the relays turn off #define relay1 2 // Set the relay drive GPIO pins to 2/3/4/5 #define relay2 3 #define relay3 4 #define relay4 5 byte incomingByte = 0x00; // This byte will hold the instructions received from the PC int relay1_state = 0; // These four integers hold the states of the relays (off = 0, and on = 1) int relay2_state = 0; // All four states start off in the 0/off state int relay3_state = 0; int relay4_state = 0; void setup() { // Let's set up comms, define the functions of the GPIO pins, and turn all relays off. Serial.begin(115200); // Set the baud rate to match the PC comms baud rate (115200) pinMode(relay1,OUTPUT); // Set relay1 - relay4 as outputs pinMode(relay2,OUTPUT); pinMode(relay3,OUTPUT); pinMode(relay4,OUTPUT); digitalWrite(relay1,HIGH); // Turn relays off. If your relay is active high, change this value to LOW digitalWrite(relay2,HIGH); digitalWrite(relay3,HIGH); digitalWrite(relay4,HIGH); } void loop(){ // This is the main program loop while (Serial.available() > 0) { // Do nothing until a serial instruction has been received from the PC incomingByte = Serial.read(); // If an instruction is available, store the value in IncomingByte if(incomingByte == 49){ // If the received value is 1 decimal/49 ASCII, then do the following if(relay1_state == 0){ // Check to see the current state of relay#1. If 0, change it to 1, and turn the relay on (low) relay1_state = 1; // Set new state to 1 (on) digitalWrite(relay1,LOW); // turn on relay#1 } else{ // If incomingByte WAS 49, BUT relay1_state was 1, then do the following: relay1_state = 0; // Chance the value back to 0 (default state/OFF) and... digitalWrite(relay1,HIGH); // Turn the relay back off. } } else if(incomingByte == 50){ // Same as above for all four relays. No need to keep commenting. 50 ASCII = 2 Decimal if(relay2_state == 0){ relay2_state = 1; digitalWrite(relay2,LOW); } else{ relay2_state = 0; digitalWrite(relay2,HIGH); } } else if(incomingByte == 51){ // if(relay3_state == 0){ relay3_state = 1; digitalWrite(relay3,LOW); } else{ relay3_state = 0; digitalWrite(relay3,HIGH); } } else if(incomingByte == 52){ // if(relay4_state == 0){ relay4_state = 1; digitalWrite(relay4,LOW); } else{ relay4_state = 0; digitalWrite(relay4,HIGH); } } else if(incomingByte == 53){ // If the incoming byte was 53 ASCII/5 decimal, turn all relays off digitalWrite(relay1,HIGH); // Turn relays off digitalWrite(relay2,HIGH); digitalWrite(relay3,HIGH); digitalWrite(relay4,HIGH); relay1_state = 0; // Set all states to default state of 0/OFF relay2_state = 0; relay3_state = 0; relay4_state = 0; } else{} // If an incoming byte was received, but it wasn't 1-5/49-53 ASCII, then do nothing } } // This is the end of the program