Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int i;
- int sign;
- long value;
- float result;
- float results = [0,0,0];
- int clockpin = [4,6,8];
- int datapin = [5,7,9];
- unsigned long tempmicros;
- int device;
- void setup() {
- Serial.begin(9600);
- for(i=0;i<sizeoff(datapin);i++) {
- pinMode(clockpin[i], INPUT);
- pinMode(datapin[i], INPUT);
- }
- }
- void loop () {
- for(i=0;i<sizeoff(datapin);i++) {
- while (digitalRead(clockpin[i])==HIGH) {} //if clock is LOW wait until it turns to HIGH
- tempmicros=micros();
- while (digitalRead(clockpin[i])==LOW) {} //wait for the end of the HIGH pulse
- if ((micros()-tempmicros)>500) { //if the HIGH pulse was longer than 500 micros we are at the start of a new bit sequence
- decode(i); //decode the bit sequence
- }
- }
- }
- void decode(int device) {
- sign=1;
- value=0;
- for (i=0;i<23;i++) {
- while (digitalRead(clockpin[device])==HIGH) { } //wait until clock returns to HIGH- the first bit is not needed
- while (digitalRead(clockpin[device])==LOW) {} //wait until clock returns to LOW
- if (digitalRead(datapin[device])==LOW) {
- if (i<20) {
- value|= 1<<i;
- }
- if (i==20) {
- sign=-1;
- }
- }
- }
- result=(value*sign)/100.00;
- results[device] = result;
- Serial.println(result,2); //print result with 2 decimals
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement