Posted by : Niam Tamami June 25, 2015

blognitbot.com


http://ediy.com.my/


Referensi : "http://ediy.com.my/Downloads/16%20Output%20Arduino%20Controller.ino" 
/*
16 Output Arduino Controller Ver. 0.1
Released on 22 Oct 2012
by ediy.com.my

SerialEvent only support Arduino ver 1.00 and above
serial command syntax must be ssnn or ss nn
where ss is a two character command, it can be ON; OF; RS; AC
where nn is the relay number, nn ranged from 1 to 16

example:
ON01 or ON1 <--turn on channel1
OF02 or OF1 <--turn off channel2
RS03 or RS3 <--get the state of channel3
AC <--toggle acknowledge ON of OFF

return 0 if output is low
return 1 if output high
return 2 if send ON successful
return 3 if send OFF successful
*/ #define RELAY1 2 //Arduino Digital 2
#define RELAY2 3 //Arduino Digital 3 PWM
#define RELAY3 4 //Arduino Digital 4
#define RELAY4 5 //Arduino Digital 5 PWM
#define RELAY5 6 //Arduino Digital 6 PWM
#define RELAY6 7 //Arduino Digital 7
#define RELAY7 8 //Arduino Digital 8
#define RELAY8 9 //Arduino Digital 9
#define RELAY9 10 //Arduino Digital 10
#define RELAY10 11 //Arduino Digital 11
#define RELAY11 12 //Arduino Digital 12
//#define RELAY12 13 //Do not use digital 13 since it will blink if usb serial connecting to computer //you can use the analog pins as digital pins, by numbering them 14 - 19.
#define RELAY12 14 //Arduino Digital 14 (Analog 0)
#define RELAY13 15 //Arduino Digital 15 (Analog 1)
#define RELAY14 16 //Arduino Digital 16 (Analog 2)
#define RELAY15 17 //Arduino Digital 17 (Analog 3)
#define RELAY16 18 //Arduino Digital 18 (Analog 4) #define REL_COUNT 16 //how many relay
int Relays[REL_COUNT] = {RELAY1, RELAY2, RELAY3, RELAY4, RELAY5, RELAY6, RELAY7, RELAY8, RELAY9, RELAY10, RELAY11, RELAY12, RELAY13, RELAY14, RELAY15, RELAY16}; String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
boolean ack = false; //whether return status after serial command received void setup()
{
for (int i=0;i<REL_COUNT;i++) {
pinMode(Relays[i], OUTPUT); // declare Relays as output
}

Serial.begin(9600);
inputString.reserve(5); //reserve 6 bytes for the inputString:
Serial.println("Controller Ready.");
} void loop() {
// process data when a carriage return arrives:
if (stringComplete) {
processData();
// clear the string:
inputString = "";
stringComplete = false;
}
} void processData ()
{
int thisRelay, val;
char relayNumber[3], cmd[3];
char inputString2Array[6]; //hold the serial data in array form
inputString.trim(); //remove whilte space
inputString.toUpperCase(); //covert to upper case
inputString.toCharArray(inputString2Array, 6); //convert string to array

// first two character is the command
cmd[0] = inputString2Array[0];
cmd[1] = inputString2Array[1];
cmd[2] = 0; //null terminate Command = Chr(data(0)) + Chr(data(1)) //next two character is the relay numner (third & forth charcter)
relayNumber[0] = inputString2Array[2];
relayNumber[1] = inputString2Array[3];
relayNumber[2] = 0; // null terminate value = Chr(data(2)) + Chr(data(3)) thisRelay = atoi(relayNumber); //convert string to integer
if (strcmp(cmd,"ON")==0)
{
digitalWrite(Relays[thisRelay-1], HIGH);
if (ack) Serial.println("2");
}
else if (strcmp(cmd,"OF")==0)
{
digitalWrite(Relays[thisRelay-1], LOW);
if (ack) Serial.println("3");
}
else if (strcmp(cmd,"RS")==0)
{
val = digitalRead(Relays[thisRelay-1]); // read the input pin, return 1 if High, return 0 if Low
Serial.println(val,DEC);
}
else if (strcmp(cmd,"AC")==0)
{
ack = ! ack;
if (ack) {
Serial.println("Acknowledge on");
} else {
Serial.println("Acknowledge off");
}
}

} // end of processData /*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\r') {
stringComplete = true;
}
}
}

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Entri Populer

tweet

- Copyright © 2013 blog.nitbot.com -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -