The display is instantiated with the following pins. I will be using the well written Adafruit display and graphics libraries. The output when the encoder is turned CW and CCW and when the switch is pressed.Īs said earlier I will be using a Nokia 5110 84x48 pixels graphic display for this tutorial. The code below is all that is required to read the encoder position as well as the select switch. Because other pins on Atmega328 will not have interrupt handling capability. However note that this code will only work if the pins 2 and 3 are used. Exit the board manager, and go to Tools > Board > Arduino Arduino ARM. We will cover more on interrupts in some other tutorial. Now we need to install Arduino SAM Boards, which in my case are installed. Also notice the use of keyword volatile for the variables used in the Interrupt functions. Since these are interrupt functions these aren't called anywhere in the setup() and loop() functions. The interrupts trigger functions PinA() and PinB() for interrupts on pins 2 and 3 respectively which eventually result in decremented or increment the count. These are setup to trigger on a rising edge of the pulse. The code uses to Interrupt pins 2 and 3 on Arduino. map() function to map its output to one of four values: 0, 1, 2, or 3. The switch statement accepts an expression and you use multiple cases statements to test an expression against the switch expression. This program first reads the photoresistor. Switch case in Arduino is just like the switch case in C language. In the Arduino IDE open the serial monitor and send the characters a, b, c, d, or e to lit up the corresponding LED, or anything else to switch them off. This tutorial shows you how to use it to switch between four desired states of a photo resistor: really dark, dim, medium, and bright. It simply increments a variable called encoderPos if the encoder is turned CW and decremented if turned CCW. The break statement at the end of each case tells the Arduino to finish with the switch case and move on with the rest of the program. Switch allows you to choose between several discrete options. I started out with hooking up just the encoder with Arduino and printing the message on the terminal. I would highly recommend you to go through the Improved Arduino Rotary Encoder Reading instructable to understand the types of encoders, the internal mechanism and all the related terminology. Which can be used to select menu items or confirm an action. Apart from this the encoder we will be using also has a select switch. Encoders will help us do all of the above. However to input time from the user, the hours will be (0 to 23), minutes and seconds will be (0 to 60). The range for the Menu will be 0 to 7 in both directions. As in case of your clock, the user may have to transverse a menu of say 8 items. What that essentially means is that the input range for your gizmo can be decided in software. I would recommend doing it step by step.Įncoders are great input devices which have infinite travel in both clockwise(CW) and anti-clockwise directions(CCW). You may want to hook-up all the things at once or do it step by step. We will test the encoder first and then the display. The basic hookup will remain same during the entire tutorial.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |