Advertisement
Guest User

tolomero

a guest
Apr 1st, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.32 KB | None | 0 0
  1.  
  2. int i;
  3. int sign;
  4. long value;
  5. float result;
  6. float results = [0,0,0];
  7. int clockpin = [4,6,8];  
  8. int datapin = [5,7,9];
  9. unsigned long tempmicros;
  10. int device;
  11.  
  12. void setup() {
  13.  
  14.   Serial.begin(9600);
  15.  
  16.   for(i=0;i<sizeoff(datapin);i++) {
  17.     pinMode(clockpin[i], INPUT);
  18.     pinMode(datapin[i], INPUT);
  19.   }
  20. }
  21.  
  22.  
  23. void loop () {
  24.  
  25.   for(i=0;i<sizeoff(datapin);i++) {
  26.     while (digitalRead(clockpin[i])==HIGH) {} //if clock is LOW wait until it turns to HIGH
  27.     tempmicros=micros();
  28.     while (digitalRead(clockpin[i])==LOW) {} //wait for the end of the HIGH pulse
  29.     if ((micros()-tempmicros)>500) { //if the HIGH pulse was longer than 500 micros we are at the start of a new bit sequence
  30.       decode(i); //decode the bit sequence
  31.     }
  32.   }
  33. }
  34.  
  35. void decode(int device) {
  36.  
  37.   sign=1;
  38.   value=0;
  39.   for (i=0;i<23;i++) {
  40.     while (digitalRead(clockpin[device])==HIGH) { } //wait until clock returns to HIGH- the first bit is not needed
  41.     while (digitalRead(clockpin[device])==LOW) {} //wait until clock returns to LOW
  42.     if (digitalRead(datapin[device])==LOW) {
  43.       if (i<20) {
  44.         value|= 1<<i;
  45.       }
  46.       if (i==20) {
  47.         sign=-1;
  48.       }
  49.     }
  50.   }
  51.   result=(value*sign)/100.00;    
  52.   results[device] = result;
  53.   Serial.println(result,2); //print result with 2 decimals
  54.   delay(1000);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement