/*
Blink
Turns an LED on for one second, then off for one second, repeatedly.
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your Arduino
model, check the Technical Specs of your board at:
modified 8 May 2014
by Scott Fitzgerald
modified 2 Sep 2016
by Arturo Guadalupi
modified 8 Sep 2016
by Colby Newman
This example code is in the public domain.
*/
#define HEADER_1 'A' //0xA5
#define HEADER_2 '5' //0x5A
#define HEADER_3 'A' //0xA5
#define HEADER_4 '5' //0x5A
#define SIZE 3
#define DATA 4
#define CHECKSUM 'C'
//제어장치 이름 5char - "Relay", "Servo"
//제어값 3char " On" "Off"
#define BUTTON 9
#define RELAY_1 7
#define RELAY_2 6
#define RELAY_3 5
#define RELAY_4 4
int relay[5] = {0,RELAY_1,RELAY_2,RELAY_3,RELAY_4};
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int DataSize;
char data[50];
int state;
int idx;
int relay_num ;
int relay_onoff;
char CheckSum();
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(RELAY_1, OUTPUT);
pinMode(RELAY_2, OUTPUT);
pinMode(RELAY_3, OUTPUT);
pinMode(RELAY_4, OUTPUT);
pinMode(BUTTON, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(BUTTON, LOW); // turn the LED on (HIGH is the voltage level)
myservo.attach(8); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
int ver = 2;
Serial.print(ver);
Serial.println("v_Start");
state = 1;
}
// the loop function runs over and over again forever
void loop() {
if (Serial.available() > 0) {
byte temp = Serial.read();
switch(state) {
case 1:
if(temp == HEADER_1) {
Serial.print("1 ");
state = 2;
idx = 0;
}
break;
case 2:
if(temp == HEADER_2) {
Serial.print("2 ");
state = 3;
}
else {
state = 1;
}
break;
case 3:
if(temp == HEADER_3) {
Serial.print("3 ");
state = 4;
}
else {
state = 1;
}
break;
case 4:
if(temp == HEADER_2) {
Serial.print("4 ");
state = 5;
}
else {
state = 1;
}
break;
case 5:
if(temp > '0' && temp <='9') { //릴레이 번호
Serial.print("5:REALAY ");
relay_num = temp-'0';
state = 6;
}
else if(temp == 'B' ) { //Button
Serial.print("5:BUTTON ");
state = 7;
}
else {
Serial.print("5 fail");
state = 1;
}
break;
case 6:
if(temp < 'A' || temp >'Z') {
state = 1;
Serial.print("6 Fail");
}
else {
Serial.print("6 ");
relay_onoff = temp;
if(relay_onoff =='T' ) {
Serial.print(relay_num);
Serial.println("ON");
digitalWrite(relay[relay_num],HIGH);
}
if(relay_onoff =='F' ) {
Serial.print(relay_num);
Serial.println("ON"); //"A5A51T"
digitalWrite(relay[relay_num],LOW);
}
state = 1;
}
break;
case 7:
if(temp >= '0' && temp <='9') {
int repeat = temp - '0';
Serial.print(repeat);
Serial.println(" repeats");
digitalWrite(BUTTON,LOW);
for (int i=0;i<repeat;i++){
digitalWrite(BUTTON,HIGH);
delay(500);
digitalWrite(BUTTON,LOW);
delay(500);
}
}
else {
state = 1;
}
break;
}
//myservo.write(180);
}
}
char CheckSum(){
int temp = 0;
for(int i=0; i < DataSize; i++){
Serial.print(data[i]-'0');
temp += (data[i]-'0');
}
Serial.println(temp);
return (temp%10);
}