Advertisement
mikroavr

caliper_digital_arduino

Feb 23rd, 2022
1,432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int bit_array[25];
  2. unsigned long time_now;
  3. int CLOCK_PIN = 5;
  4. int DATA_PIN = 4;
  5. void setup() {
  6.   Serial.begin(9600);
  7.   pinMode(CLOCK_PIN, INPUT);
  8.   pinMode(DATA_PIN, INPUT);
  9.   Serial.println("caliper ready");
  10. }
  11. void loop() {
  12.   while (digitalRead(CLOCK_PIN) == LOW) {};
  13.   time_now = micros();
  14.   while (digitalRead(CLOCK_PIN) == HIGH) {};
  15.   if ((micros() - time_now) > 500) {
  16.     //decode();
  17.   }
  18. }
  19.  
  20. void decode() {
  21.   int sign = 1;
  22.   int i = 0;
  23.   float value = 0.0;
  24.   float result = 0.0;
  25.   bit_array[i] = digitalRead(DATA_PIN);
  26.   while (digitalRead(CLOCK_PIN) == HIGH) {};
  27.   for (i = 1; i <= 24; i++) {
  28.     while (digitalRead(CLOCK_PIN) == LOW) {};
  29.     bit_array[i] = digitalRead(DATA_PIN);
  30.     while (digitalRead(CLOCK_PIN) == HIGH) {};
  31.   }
  32.   for (i = 0 ; i <= 24; i++) {
  33.     Serial.print(bit_array[i]); // print binary data output caliper
  34.     Serial.print(" ");
  35.   }
  36.   Serial.println();
  37.   for (i = 1; i <= 20; i++) {
  38.     value = value + (pow(2, i - 1) * bit_array[i]);
  39.   }
  40.   if (bit_array[21] == 1)sign = -1;
  41.   if (bit_array[24] == 1) {
  42.     result = (value * sign) / 2000.00;
  43.     Serial.print(result, 3);
  44.     Serial.println("in");
  45.   } else {
  46.     result = (value * sign) / 100.00;
  47.     Serial.print(result, 2); //Serial.print(result,2) --> untuk dua digit
  48.     Serial.println("mm");
  49.   } delay(500);
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement