For transmitting and receiving data properly and synced a technique is used its called Handshake method my program is based on the SerialCallResponse program.
Serial Call and Response program |
Here I explain how it is works
- First Arduino tries to communicate with processing IDE
- By sending A continuously through the serial port
- Here we can see that in the serial monitor
- If the processing receive A it will clear the serial port of the processing
- Then it will send the character A to the Arduino
- It is called the handshaking method
- if the first contact is established then data(1 byte) will send at a time through the serial port
- Here I'm sending the data by a delimiter (samples the data)
- I designed my oscilloscope for measure up to 4 channels, but now I'm using it for only 2 channels, that's why I've to sample the data
- The data will send one at a time if I've 4 data it will send one after one with a small duration of time
- Ok now we discuss our above diagram
- Once the initial contact is successful the Arduino sends the data by sample
- If the data is received by the processing IDE, it will clear its port
- And send the character A to the Arduino
- Again Arduino sends the value
- The processes are repeated as a loop
Now I explain the code
Arduino code
- int a = 0; // First Channel
- int b = 0; // Second Channel
These code initial the 2 channel a & b3. int inByte = 0; // incoming serial byte
It will store the value( character A ) from the processing IDE4. void setup() {
5. // start serial port at 9600 bps:
6. Serial.begin(9600);
7. while (!Serial) {
8. ; // wait for serial port to connect. Needed for native USB port only
9. }
Basically, it initializes the serial port baud rate and the 7th line meaning is, When you open the serial port of a board like the Uno or Mega the whole board usually resets*, so opening the serial port allows you to see the first bits of the serial data. On the Leonardo etc it doesn't reset when you open the serial, so any serial output during the setup() function would be missed. Adding that line makes the board pause until you open the serial port, so you get to see that initial bit of data.10. establishContact(); // send a byte to establish contact until receiver responds
It is a user-defined function. The function will be declared under the loop section11. } void loop() {
12. // if we get a valid byte, read analog ins:
13. if (Serial.available() > 0) {
14. // get incoming byte:
15. inByte = Serial.read();
These lines are continuously checking the acknowledgment from the Processing and read the serial data and store it on inByte
16. a = analogRead(A0)/4 ;// 1024/4 =255
17. b = analogRead(A1)/4;
18. delay(10);
These lines are used to get the analog values from the A0 and A1 pins and it converted into 8 bit by dividing the value by 419. Serial.write(a);
20. Serial.write(b);
21. }
22. }
Write the values to the serial port, one value at a time23. void establishContact() {
24. while (Serial.available() <= 0) {
25. Serial.print('A'); // send a capital A
26. delay(300);
27. }
28. }
These lines are the user-defined function, It basically do the initial contact with the processing IDE by sending the character A
Comments
Post a Comment