Advertisement
CuriousScientist

DRO Scale with Arduino

Oct 13th, 2019
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If you found my video helpful, please SUBSCRIBE:  https://www.youtube.com/channel/UCKp1MzuAceJnDqAvsZl_46g
  2. //The code belongs to the following tutorial video: https://youtu.be/mFWOOtJPVAs
  3.  
  4. /*
  5. Please consider buying the parts using the following links to support me:
  6. JCS-900 DRO: https://www.banggood.com/custlink/mGGv0HttR8
  7. D-SUB 9 pin 10 pcs (both): https://www.banggood.com/custlink/vm33etFthU
  8. D-SUB 9 pin extension board (Female): https://www.banggood.com/custlink/mmmmBtttR1
  9. D-SUB 9 pin extension board (Male): https://www.banggood.com/custlink/3mGK6F5FcK
  10. 16x2 LCD (I2C): https://www.banggood.com/custlink/KmGGgtP2Lv
  11. DSO-150 Oscilloscope: https://www.banggood.com/custlink/3m3D80wzMy
  12. Oscilloscope probe (1 pc): https://www.banggood.com/custlink/vDDDigTMUW
  13. Arduino UNO: https://www.banggood.com/custlink/33KKF85c3i
  14. */
  15.  
  16. //length: 50 mm = 50000 um
  17. //resolution: 5 um
  18. //steps: 50000 um / 5 um = 10000
  19. //REMEMBER! You have to calibrate your scale to get precise values in physical units
  20. //You only get increments here and you have to calibrate the increments
  21.  
  22. #define encoder0PinA  2 //Pin 2 is one of the pins which works with attachInterrupt() on Arduino UNO
  23. #define encoder0PinB  4
  24.  
  25. volatile int encoder0Pos = 0; //Position of the encoder
  26. boolean newdata = false; //Flag to see if there's new data (the encoder was moved)
  27.  
  28. #include <Wire.h> //for I2C
  29. #include <LiquidCrystal_I2C.h> //LCD's own library
  30. LiquidCrystal_I2C lcd(0x27, 16, 2); //initializing LCD
  31. //2 lines, 16 Characters per line
  32. //-----------------------------------------------------------------------
  33.  
  34. void setup() {
  35.   pinMode(encoder0PinA, INPUT); //Pin 2 = Input
  36.   digitalWrite(encoder0PinA, HIGH);       // turn on pull-up resistor
  37.   pinMode(encoder0PinB, INPUT); //Pin 4 = Input
  38.   digitalWrite(encoder0PinB, HIGH);       // turn on pull-up resistor
  39.  
  40.   attachInterrupt(0, doEncoder, CHANGE);  // encoder pin on interrupt 0 - pin 2
  41.   Serial.begin (9600);
  42.   Serial.println("DRO testing");                // start message
  43.   //------------------------------------------------------
  44.   lcd.init();                      // initialize the lcd
  45.   lcd.init();
  46.   lcd.backlight();
  47.   //------------------------------------------------------
  48.   //------------------------------------------------------
  49.   //fancy startup process, "animation" with the dots on the screen. You can skip this section
  50.   lcd.clear(); //clear lcd
  51.   lcd.setCursor(0,0); //Defining positon to write from. first row,first column .
  52.   lcd.print("DRO Scale");
  53.   lcd.setCursor(0,1); //Defining positon to write from. second row,first column .
  54.   lcd.print("Starting.");
  55.   delay(300);
  56.   lcd.setCursor(0,1);
  57.   lcd.print("Starting..");
  58.   delay(300);
  59.   lcd.setCursor(0,1);
  60.   lcd.print("Starting...");
  61.   delay(300);
  62.   lcd.setCursor(0,1);
  63.   lcd.print("Starting....");
  64.   delay(300);
  65.   lcd.setCursor(0,1);
  66.   lcd.print("Starting.....");
  67.   delay(300);
  68.   //------------------------------------------------------
  69.   //Printing the fixed text on the screen  
  70.     lcd.clear();
  71.     lcd.setCursor(0,0); //1st row, 1st place
  72.     lcd.print("DRO Scale");
  73.     lcd.setCursor(0,1); //2nd row, 1st place
  74.     lcd.print("Position: ");
  75.  
  76. }
  77.  
  78. void loop()
  79. {
  80.   if(newdata == true) //if the encoder was moved
  81.   {
  82.     lcd.clear(); //clean the screen to avoid mess (i.e. we try to print "999" after "1000", we don't want to see "9990")
  83.     lcd.setCursor(0,0);
  84.     lcd.print("DRO Scale");
  85.     lcd.setCursor(0,1);
  86.     lcd.print("Position: ");
  87.     lcd.setCursor(10,1); //we move the cursor after the "Position: " part and start printing from there
  88.     lcd.print(encoder0Pos); //we print the number based on the position
  89.   }
  90.  
  91.   newdata = false; //we switch the flag to 'false' so the Arduino will not do anything until a new data comes in
  92. }
  93.  
  94. void doEncoder() //if the attachInterrupt() is triggered, this function runs
  95. {
  96.    if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) //if the Arduino saw the square waves' rising edge
  97.   {
  98.     encoder0Pos++; // increase the position
  99.   } else {
  100.     encoder0Pos--; //otherwise decrease the position
  101.   }
  102.   newdata = true; //we change this flag, so the if statement in the loop() will run and the data will be printed
  103.   Serial.println(encoder0Pos, DEC); //sending the data to the PC. This can be moved in to the loop()'s if()  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement