-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialControl.ino
128 lines (119 loc) · 3.33 KB
/
SerialControl.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
double a;
boolean flag;
String Sinput="";
void setup(){
Serial.begin(38400);
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
// for(int i=2;i<13;i++){ // lembrar de mudar para o MEGA
// pinMode(i,OUTPUT);
// } // configure the PWM outputs
// for(int i=22;i<=52;i=i+2){
// pinMode(i,INPUT);
// }// configure the digital inputs
//
// for(int i=23;i<=53;i=i+2){
// pinMode(i,OUTPUT);
// } // configure the digital outputs
}
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
double str2int(String str){
int L,k;
double R;
L=str.length()-2;
R=0;
for(k=L;k>=0;k--){
R+=pow(10,k)*(str.charAt(L-k)-48);
}
return R;
}
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
void Command(String str){
int dur,k,indm,indf,inds,port,val;
static int Fport;
String Saux,Sout;
Sout="";
k=str.indexOf('/');
inds=k+1;
Saux=str.substring(0,k);
//Serial.println(str);
k=0;
//---------------------Analog Outputs--------------------
if(Saux=="PWM"){
indf=str.indexOf('/',inds)+1;
while(indf>0){
indm=str.indexOf('>',inds)+1;
port=str2int(str.substring(inds,indm));
val=str2int(str.substring(indm,indf));
inds=indf;
indf=str.indexOf('/',inds)+1;
analogWrite(port,val);
}
}
//---------------------Analog Inputs-----------------------
else if(Saux=="AI"){
indf=str.indexOf('/',inds)+1;
while(indf>0){
port=str2int(str.substring(inds,indf));
inds=indf;
indf=str.indexOf('/',inds)+1;
Sout+=analogRead(port);
delay(10);
Sout+=';';
}
Serial.println(Sout);
}
//----------------------Digital Inputs---------------------
else if(Saux=="DI"){
indf=str.indexOf('/',inds)+1;
while(indf>0){
port=str2int(str.substring(inds,indf));
inds=indf;
indf=str.indexOf('/',inds)+1;
Sout+=digitalRead(port);
delay(10);
Sout+=';';
}
Serial.println(Sout);
}
//-----------------------Digital Outputs---------------------
else if(Saux=="DO"){
indf=str.indexOf('/',inds)+1;
while(indf>0){
indm=str.indexOf('>',inds)+1;
port=str2int(str.substring(inds,indm));
val=str2int(str.substring(indm,indf));
inds=indf;
indf=str.indexOf('/',inds)+1;
digitalWrite(port,val);
}
}
//----------------------Arduino Identification---------------
else if(str=="*IDN?\n"){
Serial.println("Arduino MEGA-1280 programmed by Jhonatam C. Rodrigues");
Serial.println("****Commands****");
Serial.println("PWM/port1>val1/port2>val2/.../\n");
Serial.println("DO/port1>val1/port2>val2/.../\n");
Serial.println("AI/port1/port2/port3/.../\n");
Serial.println("DI/port1,port2,port3/.../\n");
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void loop(){
char Ci;
if(Serial.available()){
Ci=Serial.read();
Sinput+=Ci;
if(Ci=='\n'){
Command(Sinput);
Sinput="";
}
}
}