Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //If you found my video helpful, please SUBSCRIBE: https://www.youtube.com/channel/UCKp1MzuAceJnDqAvsZl_46g
- //The code belongs to the following tutorial video: https://youtu.be/mFWOOtJPVAs
- /*
- Please consider buying the parts using the following links to support me:
- JCS-900 DRO: https://www.banggood.com/custlink/mGGv0HttR8
- D-SUB 9 pin 10 pcs (both): https://www.banggood.com/custlink/vm33etFthU
- D-SUB 9 pin extension board (Female): https://www.banggood.com/custlink/mmmmBtttR1
- D-SUB 9 pin extension board (Male): https://www.banggood.com/custlink/3mGK6F5FcK
- 16x2 LCD (I2C): https://www.banggood.com/custlink/KmGGgtP2Lv
- DSO-150 Oscilloscope: https://www.banggood.com/custlink/3m3D80wzMy
- Oscilloscope probe (1 pc): https://www.banggood.com/custlink/vDDDigTMUW
- Arduino UNO: https://www.banggood.com/custlink/33KKF85c3i
- */
- //length: 50 mm = 50000 um
- //resolution: 5 um
- //steps: 50000 um / 5 um = 10000
- //REMEMBER! You have to calibrate your scale to get precise values in physical units
- //You only get increments here and you have to calibrate the increments
- #define encoder0PinA 2 //Pin 2 is one of the pins which works with attachInterrupt() on Arduino UNO
- #define encoder0PinB 4
- volatile int encoder0Pos = 0; //Position of the encoder
- boolean newdata = false; //Flag to see if there's new data (the encoder was moved)
- #include <Wire.h> //for I2C
- #include <LiquidCrystal_I2C.h> //LCD's own library
- LiquidCrystal_I2C lcd(0x27, 16, 2); //initializing LCD
- //2 lines, 16 Characters per line
- //-----------------------------------------------------------------------
- void setup() {
- pinMode(encoder0PinA, INPUT); //Pin 2 = Input
- digitalWrite(encoder0PinA, HIGH); // turn on pull-up resistor
- pinMode(encoder0PinB, INPUT); //Pin 4 = Input
- digitalWrite(encoder0PinB, HIGH); // turn on pull-up resistor
- attachInterrupt(0, doEncoder, CHANGE); // encoder pin on interrupt 0 - pin 2
- Serial.begin (9600);
- Serial.println("DRO testing"); // start message
- //------------------------------------------------------
- lcd.init(); // initialize the lcd
- lcd.init();
- lcd.backlight();
- //------------------------------------------------------
- //------------------------------------------------------
- //fancy startup process, "animation" with the dots on the screen. You can skip this section
- lcd.clear(); //clear lcd
- lcd.setCursor(0,0); //Defining positon to write from. first row,first column .
- lcd.print("DRO Scale");
- lcd.setCursor(0,1); //Defining positon to write from. second row,first column .
- lcd.print("Starting.");
- delay(300);
- lcd.setCursor(0,1);
- lcd.print("Starting..");
- delay(300);
- lcd.setCursor(0,1);
- lcd.print("Starting...");
- delay(300);
- lcd.setCursor(0,1);
- lcd.print("Starting....");
- delay(300);
- lcd.setCursor(0,1);
- lcd.print("Starting.....");
- delay(300);
- //------------------------------------------------------
- //Printing the fixed text on the screen
- lcd.clear();
- lcd.setCursor(0,0); //1st row, 1st place
- lcd.print("DRO Scale");
- lcd.setCursor(0,1); //2nd row, 1st place
- lcd.print("Position: ");
- }
- void loop()
- {
- if(newdata == true) //if the encoder was moved
- {
- lcd.clear(); //clean the screen to avoid mess (i.e. we try to print "999" after "1000", we don't want to see "9990")
- lcd.setCursor(0,0);
- lcd.print("DRO Scale");
- lcd.setCursor(0,1);
- lcd.print("Position: ");
- lcd.setCursor(10,1); //we move the cursor after the "Position: " part and start printing from there
- lcd.print(encoder0Pos); //we print the number based on the position
- }
- newdata = false; //we switch the flag to 'false' so the Arduino will not do anything until a new data comes in
- }
- void doEncoder() //if the attachInterrupt() is triggered, this function runs
- {
- if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) //if the Arduino saw the square waves' rising edge
- {
- encoder0Pos++; // increase the position
- } else {
- encoder0Pos--; //otherwise decrease the position
- }
- newdata = true; //we change this flag, so the if statement in the loop() will run and the data will be printed
- Serial.println(encoder0Pos, DEC); //sending the data to the PC. This can be moved in to the loop()'s if()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement