Beruflich Dokumente
Kultur Dokumente
4 Port UART
Arduino MEGA sample code
Revised 7/10/15
//1:r<CR>
//2:i<CR>
//3:c<CR>
//4:r<CR>
//To open a channel and not send a command just send the channel number followed by a colon.
//1:<CR>
//3:<CR>
5V
MADE IN
ITALY 22
GND
9
8
7
6
5
4
3
2
1
0
13
12
10
11
AREF
SDA 20
RX3 15
RX2 17
RX1 19
SCL 21
TX3 14
TX2 16
TX1 18
24
PWM
RX0
TX0
26
L COMMUNICATION 28
30 31
TX
ON 32 33
RX
34 35
36 37
38 39
40 41
42 43
44 45
MEGA 46 47
DIGITAL
48 49
50 51
52 53
POWER ANALOG IN
RESET
IOREF
GND
GND
A10
A12
A13
A14
A15
A11
3V3
GND
VIN
A0
A1
A2
A3
A4
A5
A7
A8
A9
5V
X Y PRB
0 0 1
0 1 2
1 0 3
1 1 4
char computerdata[20]; //A 20 byte character array to hold incoming data from a pc/mac/other
char sensordata[30]; //A 30 byte character array to hold incoming data from the sensors
byte computer_bytes_received=0; //We need to know how many characters bytes have been received
byte sensor_bytes_received=0; //We need to know how many characters bytes have been received
void setup(){
Serial.begin(9600); //set baud rate for the hardware serial port 0 to 9600
Serial1.begin(9600); //set baud rate for the hardware serial port 1 to 9600
pinMode(Pin_x, OUTPUT); //Set the digital pin as output.
pinMode(Pin_y, OUTPUT); //Set the digital pin as output.
}
void serialEvent(){
computer_bytes_received=Serial.readBytesUntil(13,computerdata,20); //This interrupt will trigger when the data coming from
computerdata[computer_bytes_received]=0; //the serial monitor(pc/mac/other) is received
} //We read the data sent from the serial monitor
//(pc/mac/other) until we see a <CR>.
//We also count how many characters have been received
//We add a 0 to the spot in the array just after the last
//character we received.. This will stop us from
//transmitting incorrect data that may have been left
//in the buffer
void serialEvent1(){
sensor_bytes_received=Serial1.readBytesUntil(13,sensordata,30); //This interrupt will trigger when the data coming from
sensordata[sensor_bytes_received]=0; //the serial monitor(pc/mac/other) is received
} //we read the data sent from the Atlas Scientific device
//until we see a <CR>. We also count how many character
//have been received
//we add a 0 to the spot in the array just after the last
//character we received. This will stop us from
//transmitting incorrect data that may have been left
//in the buffer
void loop(){
if(sensor_bytes_received!=0){ //If data has been transmitted from an Atlas Scientific device
Serial.println(sensordata); //let’s transmit the data received from the Atlas Scientific device to the serial monitor
sensor_bytes_received=0; //Reset the var sensor_bytes_received to equal 0
}
}
case '2':
digitalWrite(Pin_x, LOW);
digitalWrite(Pin_y, HIGH);
break;
case '3':
digitalWrite(Pin_x, HIGH);
digitalWrite(Pin_y, LOW);
break;
case '4':
digitalWrite(Pin_x, HIGH);
digitalWrite(Pin_y, HIGH);
break;
}