Go Down

Topic: Reading both encoders and switches at the same time. (Read 225 times) previous topic - next topic

Mty

Hi all
Im new to this so be patient.  Im making a project that requires an encoder adjustment then a button press to change OLED screens.
I have the OLED showing the values from the encoder but it looks like the code just sits there paused waiting for an encoder change so its not scanning the switches.
How can I get it to do both ....please make it simple guys.

Kralik

Please post some code so we can help you :)

Mty


#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Encoder.h>
Encoder myEnc(2, 3);
#define OLED_RESET 4

Adafruit_SSD1306 display(OLED_RESET);

// encoder variables
int newPos;
int oldPos;


#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup()   {               
  Serial.begin(9600);
   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
   display.display();
  delay(800);
  display.clearDisplay();


  // this sets up the screen and just displays rubish to check its working
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,5);
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.print("0x");
  display.println(0xDEADBEEF, HEX);
  display.display();
  delay(2000);
  display.clearDisplay();
 
}

void loop()

// I need to be able to read any of the 3 buttons
//  sw1,sw2,sw3 then act on the result accordingly
// hope this helps ...my first real project.

//   Im using an UNO


 {
//  read encoder
  long newPos = myEnc.read();
  if (newPos != oldPos) {
    oldPos = newPos;
//  disply data on OLED screen
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
//  display the fuction of the 3 buttons   
  display.println("RST      DIR       GO");
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(20,10);
//   Display the value of the encoder
  display.println(newPos);
  display.display();
  delay(1);
  display.clearDisplay();
 
  }
   
}

Delta_G

That code doesn't block.  You just need to add the digitalRead statements to read your switches and whatever code you want to run in response. 

Have you tried it yet? Give it a go and if it doesn't work out we will help you.  Don't be scared. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Go Up