Arduino display and rodory encoder
4 hours ago, Symbolls said:Hi thx for the time to answer but it dont understand how i can implement this in the code bechose i am noob to programing and i cant figure it out. I have benn resercing about this and i am just to dumb.
yeah sure, here's some readings on what I did, they're fairly basic c++ concepts https://www.geeksforgeeks.org/decision-making-c-c-else-nested-else/
http://www.cplusplus.com/doc/tutorial/variables/ https://www.geeksforgeeks.org/switch-statement-cc/
^these two sites got me through my CS 100 class a couple years ago, absolute god sends. Whenever you see some code that you don't understand, google it and you'll find a ton of information on it. To get the encoder working, have a look at this video
I'll also comment line by line what that little snippet of code does
//this should just be part of your program to handle the UI element of the project, you'll need more stuff to get the screen and whatever else working. int select = 0;//declare a variable to keep track of what menu state we're in. We can add 1 to this value by //triggering some input to the system, then based on its value decide what other parts of // the program do int max = x; //maximum number of states you want in the program, or you could also use this as a maximum number of menu screens //this bit of code reads the push button on the encoder and uses it to decide when to increment the select variable. //the encoder has what's called a pullup resistor, meaning that in the unpressed state, the signal from the encoder is high, or 5v //when the button is pushed, the switch pin is connected to ground, pulling the signal to 0v or low. if(digitalRead(SW)==LOW){//I took a bit of liberty here, I made up the variable SW for switch. Whatever pin you plug the button into on your //arduino should be put here. It's nice to use a variable that you declare globally (google C++ global variable) //so that when you read through the code you know what is being read because you used a descriptive variable name //instead of a cryptic number that correseponds to a pin select++;//++ adds one to whatever variable if(select>max){//this if statement just resets the select variable to 0 after you try to go past the last state select = 0; } } //this is the fun part. Switch cases are super powerful for things like this project because they can cycle through however many states as you define //to do whatever you want. So when the select variable = 1, the code executes what is written at case 1. Once it finishes that snippet of code, it //reaches the break; statement, which breaks out of the switch case for that iteration of the program. If you don't have the break statement, the //code will keep running line by line through the other cases, which is generally not the desired effect. switch(select){ case 1://state 1 break; case 2://state 2 break; case n: //nth state break; }
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now