Advertisement
MateuszJasek

Arduino Ohmmeter - Poprawiony

Aug 29th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include <LiquidCrystal_I2C.h>
  2.  
  3. LiquidCrystal_I2C lcd(0x27, 20, 4);
  4.  
  5. const int res0 = 5, res1 = 4, res2 = 3, res3 = 2, analog = 7;
  6. const double toVoltage = 0.004887586;
  7.  
  8. void setup() {
  9.   const byte ohmChar[] = {0x0E, 0x11, 0x11, 0x11, 0x0A, 0x0A, 0x1B, 0x00};
  10.  
  11.   pinMode(res0, OUTPUT);
  12.   pinMode(res1, OUTPUT);
  13.   pinMode(res2, OUTPUT);
  14.   pinMode(res3, OUTPUT);
  15.  
  16.   digitalWrite(res0, LOW);
  17.   digitalWrite(res1, LOW);
  18.   digitalWrite(res2, LOW);
  19.   digitalWrite(res3, LOW);
  20.  
  21.   lcd.begin();
  22.   lcd.createChar(0, ohmChar);
  23.   lcd.home();
  24. }
  25.  
  26. void loop() {
  27.   int analogVal0, analogVal1, analogVal2, analogVal3, resistor;
  28.   double Vout, ohm;
  29.  
  30.   digitalWrite(res0, HIGH);
  31.   pinMode(res1, INPUT);
  32.   pinMode(res2, INPUT);
  33.   pinMode(res3, INPUT);
  34.   analogVal0 = analogRead(analog);
  35.   delay(25);
  36.  
  37.   pinMode(res0, INPUT);
  38.   digitalWrite(res1, HIGH);
  39.   pinMode(res2, INPUT);
  40.   pinMode(res3, INPUT);
  41.   analogVal1 = analogRead(analog);
  42.   delay(25);
  43.  
  44.   pinMode(res0, INPUT);
  45.   pinMode(res1, INPUT);
  46.   digitalWrite(res2, HIGH);
  47.   pinMode(res3, INPUT);
  48.   analogVal2 = analogRead(analog);
  49.   delay(25);
  50.  
  51.   pinMode(res0, INPUT);
  52.   pinMode(res1, INPUT);
  53.   pinMode(res2, INPUT);
  54.   digitalWrite(res3, HIGH);
  55.   analogVal3 = analogRead(analog);
  56.   delay(25);
  57.  
  58.   lcd.clear();
  59.   lcd.setCursor(0, 3);
  60.  
  61.   if(analogVal0 <= 500) {
  62.     Vout = analogVal0 * toVoltage;
  63.     ohm = Vout / ((5-Vout) / 100);
  64.     resistor = 0;
  65.    
  66.    
  67.   } else if(analogVal0 > 500 && analogVal1 <= 500) {
  68.     Vout = analogVal1 * toVoltage;
  69.     ohm = Vout / ((5-Vout) / 1000);
  70.     resistor = 1;
  71.    
  72.    
  73.   } else if(analogVal0 > 500 && analogVal1 > 500 && analogVal2 <= 500) {
  74.     Vout = analogVal2 * toVoltage;
  75.     ohm = Vout / ((5-Vout) / 10000);
  76.     resistor = 2;
  77.    
  78.   } else if(analogVal0 > 500 && analogVal1 > 500 && analogVal2 > 500) {
  79.     Vout = analogVal3 * toVoltage;
  80.     ohm = Vout / ((5-Vout) / 100000);
  81.     resistor = 3;
  82.   }
  83.  
  84.   lcd.setCursor(0, 0);
  85.  
  86.   if(ohm >= 0 && ohm < 1000) {
  87.     lcd.print(ohm);
  88.   } else if(ohm >= 1000 && ohm < 1000000) {
  89.     lcd.print(ohm/1000);
  90.     lcd.print('k');
  91.   } else if(ohm >= 1000000) {
  92.     lcd.print(ohm/1000000);
  93.     lcd.print('M');
  94.   }
  95.    
  96.    
  97.   lcd.write(0);
  98.  
  99.    
  100.   lcd.setCursor(13, 0);
  101.   lcd.print("V0:");
  102.   lcd.print(analogVal0);
  103.    
  104.   lcd.setCursor(13, 1);
  105.   lcd.print("V1:");
  106.   lcd.print(analogVal1);
  107.    
  108.   lcd.setCursor(13, 2);
  109.   lcd.print("V2:");
  110.   lcd.print(analogVal2);
  111.    
  112.   lcd.setCursor(13, 3);
  113.   lcd.print("V3:");
  114.   lcd.print(analogVal3);
  115.  
  116.   lcd.setCursor(0, 3);
  117.   switch(resistor) {
  118.     case 0: lcd.print("100");   break;
  119.     case 1: lcd.print("1k");    break;
  120.     case 2: lcd.print("10k");   break;
  121.     case 3: lcd.print("100k");  break;
  122.   }
  123.   lcd.write(0);
  124.  
  125.   delay(1000);
  126.  
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement