Hardware Reference
In-Depth Information
Listing 16-2 continued
#define CK_ENC 13
#define MISO_ENC 12
#define MOSI_ENC 11
long int time;
byte npRead = 4; // number of detectors to read
float th [] = {0.0, 0.0, 0.0, 0.0 }; // angle reading
int dig1 [] = { 0, 0, 0, 0};
int dig2 [] = { 0, 0, 0, 0};
byte ledRed [] = {2, 4, 6, 8}; // pins controlling red
byte ledGreen [] = {3, 5, 7, 9}; // pins controlling green
int count =1; // LED test pattern
int error = 1; // error LED test count
void setup(){
Serial.begin(9600);
pinMode(CS_ENC, OUTPUT);
digitalWrite(CS_ENC, HIGH);
pinMode(CK_ENC, OUTPUT);
digitalWrite(CK_ENC, HIGH); // set clock high
pinMode(MOSI_ENC, OUTPUT);
digitalWrite(MOSI_ENC, LOW);
pinMode(MISO_ENC, INPUT);
pinMode(A5, OUTPUT);
for(int i=0; i<4; i++){ // set up LEDs
pinMode(ledRed[i], OUTPUT);
digitalWrite(ledRed[i], LOW);
pinMode(ledGreen[i], OUTPUT);
digitalWrite(ledGreen[i], LOW);
}
time = millis() + 2000; // reading every 2 seconds
}
void loop(){
if(millis() > time){ // only take reading every 2 seconds
time = millis() + 1000;
encRead(); // read in all sensors
for(int i =0; i<npRead; i++){ // print them out
Serial.print(th[i]); // angle
Serial.print(“ -> “);
Serial.print(dig1[i],HEX); // ready
Serial.print(“ -> “);
Search WWH ::




Custom Search