Hardware Reference
In-Depth Information
The Final Arduino Code
Now it is time to program the Arduino for the real job. he Harmo Arduino code is designed
to send data to the Raspberry Pi only when it is asked to start. he Pi sends commands to the
Arduino that consist of just a single letter. hese are Start Sending, Stop Sending and instruc-
tions on how to process the pendulum data. So type the code in Listing 16-3 into the Arduino
IDE and save it under the name Harmo .
Listing 16-3 The Harmo Arduino Code
/* Harmonograph reading four rotary encoder
By Mike Cook Feb-April 2013
Bit banging the SPI
Sending out to serial port as two 10 bit values split into two
5 bit values with the top two bits used to identify the byte
Serial data flow and configuration control
*/
#define CS_ENC 10
#define CK_ENC 13
#define MISO_ENC 12
#define MOSI_ENC 11
// constants for scaling the output
#define OFFSET 400.0 // centre of the display
#define H_OFFSET 200.0 // half offset
#define RANGE 540.0 // range +/- of co-ordinates
#define H_RANGE 270.0
// half the x range when using two pendulums
long int interval, timeToSample;
float th [] = {0.0, 0.0, 0.0, 0.0 }; // angle reading
// change the line below to your own measurements
int offset[] = {521, 510, 380, 477}; // offsets at zero degrees
int reading[4]; // holding for raw readings
boolean active = false, calabrate = false;
byte np = 2; // number of pendulums to output
byte npRead = 4; // number of detectors to read
byte ledRed [] = {2, 4, 6, 8}; // pins controlling red
byte ledGreen [] = {3, 5, 7, 9}; // pins controlling green
void setup(){
Serial.begin(115200);
pinMode(CS_ENC, OUTPUT);
continued
Search WWH ::




Custom Search