Hardware Reference
In-Depth Information
digitalWrite(second , stateChang); // change 2nd pin
delay (2); // delay befor changning next pin at highest speed
click = false ; // reset
}
delay (100); // slowing the code down = moving encoder slower
}// end void loop()
Verifying the Code
With everything set up, plug the reader Arduino into the computer and start the serial monitor. The reader prints the
count when pin 2 transitions low, decrementing or incrementing the count depending on the incoming signal. The
sensor Arduino will send one-half of the gray code per button press. If a button is held down, a continuous signal will
be sent at a maximum rate of 208ms, as defined in the code. When the code is running and the buttons are not being
pressed, the Arduino will be held in the last state. Using this sensor simulation is very helpful in debugging control
code for robots CNC or any system using control loops.
if an oscilloscope is not available to visualize what happens in the sensor code, increase all the delays to about
200ms and replace the reader with two leDs.
Note
Serial Sensors
Serial communication is one of the cornerstone communication types in computer engineering, and many sensors
communicate via this method. Serial sensors are capable of sending and receiving more information than analog
sensors by sending data by the byte. Setting up a simulated serial sensor is simple on the Arduino using the built-in
serial functions or with software serial. The trick is matching the baud rate and the actual data being sent; the
specifications should be available on the sensor's data sheet. It is recommended that software serial be used so that
the other serial connection is still available for control and monitoring.
Outputting Serial Data
The sensor for this section is the blue Parallax RFID reader that transmits serial at a baud of 2400. The RFID reader
reads special tags that contain a 40-bit identifier that is transmitted as ten hexadecimal numbers converted to plain
ASCII. A byte with a value of 10 is sent at the beginning of the tag code and is ended by a byte value of 13; there is
also a pin to activate the RFID reader. The code for the Arduino to access the RFID information is available at
http://arduino.cc/playground/Learning/PRFID , in the section modified by Worapoht K. using software serial.
Upload Listing 6-6 to the Arduino that will be used for retrieving the RFID data.
Listing 6-6. Worapoht K. Code with Reworked Comments
#include <SoftwareSerial.h>
int val = 0; // temporary holder
char code[10]; // the Tag ID
int bytesread = 0; // byte count
#define rxPin 8 // RFID reader SOUT pin
#define txPin 9 // no connection
void setup() {
Serial.begin(2400); // Hardware serial for Monitor 2400bps
 
 
Search WWH ::




Custom Search