Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://aleksandarhaber.com/incremental-rotary-encoders-with-raspberry-pi-lpd3806-600bm/
- /*This project depends on the now depreciated wiring pi project
- * wget https://project-downloads.drogon.net/wiringpi-latest.deb
- * or a working version is also saved the the nanocluster server
- * g++ -Wall -o "%e" "%f" -lwiringPi -lwiringPiDev
- * */
- #include <wiringPi.h>
- #include <string>
- #include <iostream>
- using namespace std;
- long encoder0;
- long encoder1;
- int stateA=0;
- int stateB=0;
- int angle0=0;
- int angle1=0;
- int OldAngle0=0;
- int OldAngle1=0;
- //File dataFile;
- string dataString = "";
- //hardware IO
- //may require "gpio mode <pin> in"
- /*Encoders*/
- int wireA=0; //BCM 17
- int wireB=2; //BCM 27
- int wireC=12; //MOSI
- int wireD=13; //MISO
- /*Buttons*/
- const int buttonAB = 7; //GPIO7 A-B-encoder pin7 "gpio mode 7 in"
- const int buttonCD = 21; //GPIO21 (SCLK) C-D encoder "gpio mode 21 in"
- void A();
- void B();
- void C();
- void D();
- void setup(); //sets up hardware may do all setup including fileIO in teh future
- void resetImg(); //sets data in svg poly line to zero
- int main(){
- setup();
- while(1){
- angle0=encoder0;
- angle1=encoder1;
- if (digitalRead(buttonAB) == LOW) {
- cout << "Deleting image data\n";
- dataString="";
- delay(100);
- }
- if (digitalRead(buttonCD) == LOW) {
- cout << dataString <<"\n";
- delay(100);
- }
- if (angle0==OldAngle0 && angle1==OldAngle1){
- ; //dont do shit if its not changing
- }else{
- dataString += ", ";
- dataString += to_string(angle0);
- dataString += ", ";
- dataString += to_string(angle1);
- OldAngle0=angle0;
- OldAngle1=angle1;
- cout<<"Angle is:"<<angle0 <<"\t" << angle1 <<endl;
- delay(100); //read less
- }
- }
- return 0;
- }
- void resetImg(){
- }
- void setup(){
- wiringPiSetup();
- //isr is an interupt signal, should respond well to random IO
- wiringPiISR(wireA, INT_EDGE_BOTH,&A);
- wiringPiISR(wireB, INT_EDGE_BOTH,&B);
- wiringPiISR(wireC, INT_EDGE_BOTH,&C);
- wiringPiISR(wireD, INT_EDGE_BOTH,&D);
- pinMode(buttonAB, INPUT);
- pinMode(buttonCD, INPUT);
- }
- void A(){
- if (stateA==stateB){
- encoder0++;
- }
- stateA=digitalRead(wireA);
- return;
- }
- void B(){
- if (stateA==stateB){
- encoder0--;
- }
- stateB=digitalRead(wireB);
- return;
- }
- void C(){
- if (stateA==stateB){
- encoder1++;
- }
- stateA=digitalRead(wireA);
- return;
- }
- void D(){
- if (stateA==stateB){
- encoder1--;
- }
- stateB=digitalRead(wireB);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement