Problem with Encoder and Pushbutton Polling SSD1327 Screen and Countdowntimer

Status
Not open for further replies.

Bastiaan

Well-known member
Hello fellow designers,

currently I am using a teensy3.2 with a 3pdt switch, 1 encremental encoder with a integrated pushbutton to navigate through a 3 window Menu. which works fine. I am using the bounce.h and encoder.h to access the encoder and the button.

in Menu 1 I get into external mode, and let the BNC input drive the state of the led On Off. (intensity of the LED is done by the encoder scaled accordingly).
in Menu 2 I use a CountdownTimer written by Andrew Mascolo.
in Menu 3 I use the blink without delay example for independent on off of the led.

all three menus are accessable by switching the 3pdt in the required position.

in each Mode I first check if the pushbutton has been pressed by digitalRead.
if I get into this if statement I check the encoder. and write the value to the screen.
i am doing this for menu 2 and 3.

Testing the Encoder.h and bounce.h seperate shows me that I get a nice response on the serial monitor.
however in combination with the ssd1327 Oled Screen and pushbutton encoder and 3pdt, I cant get the pushbutton to act accordingly and save the value and move on anybody an idea?
 
Without posted code showing the issue any ideas would likely not relate to that code - and perhaps details of the pins in use. Not even sure if either encoder or bounce are working separately 'together' or 'alone' - and if either was tried alone with added display?

If started in Menu #3 state does the LED Blink work properly with the right on/off cycle times?
 
So I discovered where it goes wrong,

its exactly at the clk.update();
i pasted a serial.print command before the if(clk.update())
I think I require a break in polling the pushbutton and then sending the data to the screen.


Code:
#define ENCODER_OPTIMIZE_INTERRUPTS

/*
 * In Total there are three programs
 * 1 CountDown Timer with hours minutes and seconds Time Stamp
 * 2 Intern
 * 
 * 
 * 
 * 
 */

#include <stdio.h>
#include <Arduino.h>
#include <SPI.h>
#include <U8g2lib.h>
#include <ADC.h>

#include <Encoder.h>

#include <Bounce.h>

#include <CountUpDownTimer.h>



///Function CountDownTimer
int PWMValue=0;

byte menuCount = 1;
byte IntCount= 1;
byte dir = 0;
bool runState = false;
bool runState2 = false;

long oldPosition  = -999;
long newPosition;

long oldPWMPosition=-999;
long PWMPosition;

unsigned long Hours=0;
unsigned long Minutes=0;
unsigned int long Seconds=0;
unsigned int long Hours2=0;
unsigned int long Minutes2=0;
unsigned int long Seconds2=0;

int awake;


//Switch Case between states 1 ,2,3
const int ledPin = 13;
const int CountDownPin=21;
const int IntervalPin=22;
const int ExternPin=23;
const int buttonPin  = 6;

int StateInput=0; //case switch needs to be global.

boolean OldSwitchState1 = LOW;
boolean OldSwitchState2 = LOW;
boolean OldSwitchState3 = LOW;

boolean newSwitchState1 = LOW;
boolean newSwitchState2 = LOW;
boolean newSwitchState3 = LOW;


//analog PWM control
int Pin_25_OUT_PWM=25; //out
const int Pot_Val_A12=A12; //in
int MapA12=0;

int lastseconds;

unsigned int count = 0;            // how many times has it changed to low
unsigned long countAt = 0;         // when count changed
unsigned int countPrinted = 0; 

unsigned int icount = 0;            // how many times has it changed to low
unsigned long icountAt = 0;         // when count changed
unsigned int icountPrinted = 0;



//Function Interval Timer

int ON_Time1=0;
int OFFTime1=0;
unsigned long PreviousMillis=0;

int ON_LT3760=LOW;


CountUpDownTimer T(DOWN),T2(DOWN);
Encoder Timer_1(4,3), PWM(2,1);

Bounce clk = Bounce(buttonPin, 10);  // 10 ms debounce

ADC *adc = new ADC(); // adc object
U8G2_SSD1327_MIDAS_128X128_F_4W_SW_SPI u8g2(U8G2_R2, 13, 11, 10, 9, 8);// clock= 13,  data= 11,  cs= 10,  dc= 9,  reset= 8);


int TimeControlEncoder() //
{
  newPosition = Timer_1.read();
  if (newPosition != oldPosition) 
    {
      oldPosition = newPosition;
      return newPosition/4;
      Serial.print("TimerEncoder Position ");
      Serial.println(newPosition);
  }
}

int PWMControlEncoder() //
{
  PWMPosition = PWM.read();
  if (PWMPosition != oldPWMPosition) 
    {
      oldPWMPosition = PWMPosition;
      return PWMPosition/4;
      Serial.print("PWMPosition ");
      Serial.println(PWMPosition);
  }
}


int  PWMcontrol()
{
 PWMValue = PWMControlEncoder();
 MapA12=map(PWMValue,0,4096,4096,0); //PWM should be kept above 50us 200Hz=5ms 5ms/4096 =1,22us == 1,22x42>50us
 analogWrite(25,MapA12); //200 is to be replaced by a analogRead //just a test
}


int Menu1Check() //Menu Check 1 for CountDown Timer 1 and 2, 
{
 Serial.println("in the Menu1Check");
 delay(50);
  if (clk.update()) 
  {
    
    Serial.println(" updated ");
    if(clk.fallingEdge())
    {
      
      menuCount=menuCount+1; //move statement after pressed
      count++;
      countAt=millis();
      
    } 
     
      
  }else
    {
      if(count!=countPrinted)
      {
        unsigned long nowMillis=millis();
        if(nowMillis - countAt>100)  //shortened the bounce interval to increase the speed of the button
        {
         countPrinted=count;
         
        }
      }
        //do nothing, 
      //     Serial.print("Button not Pressed ");
    }

  
  if (menuCount>10)  //limit the menu
  {Serial.print("menu larger as 10");
    menuCount = 1;
    
  }

  if (menuCount == 1) 
  {
    
     TimeControlEncoder();
 
  
  
    
    if(newPosition>0&&newPosition<59)
    {
      Hours = newPosition;
   // Serial.print(" Hours ");
  //  Serial.println(Hours);
       //out of boundary set to zero.
    }
    else Hours=0;
  }
  if (menuCount == 2) 
  { Serial.print("Menu2"); 
    newPosition=TimeControlEncoder();
 
  
    
    if(newPosition>0&&newPosition<59)
    {
      Minutes = newPosition;
  //    Serial.print(" Minutes ");
  //    Serial.println(Minutes);
  
       }else Minutes=0;
       
    
  }
  if (menuCount == 3) 
  {Serial.print("Menu3"); 
     newPosition=TimeControlEncoder();
  
    
    
    if(newPosition>0&&newPosition<59)
    {
      Seconds = newPosition;
    //     Serial.print(" Seconds ");
   //   Serial.println(Seconds);
   
      }else Seconds=0;
  } 
  if(menuCount==4)
  {
    
   
   newPosition=TimeControlEncoder();
  
    if(newPosition>0&&newPosition<59)
    {
      Hours2 = newPosition;
    
    }
    else Hours2=0;
  }
 if(menuCount==5)
  {
    newPosition=TimeControlEncoder();
 
  
    if(newPosition>0&&newPosition<59)
    {
      Minutes2 = newPosition;
     
    }else Minutes2=0;
  }
 if(menuCount==6)
  {
     newPosition=TimeControlEncoder();
  
    
    
    if(newPosition>0&&newPosition<59)
    {
      Seconds2 = newPosition;
 //     Serial.print(" Seconds2 ");
 //     Serial.println(Seconds2);
       
      }else Seconds2=0;
  }
  if(menuCount==7)
  {
    Serial.println("set timer 1");
    T.SetTimer(Hours,Minutes,Seconds); 
    T2.SetTimer(Hours2,Minutes2,Seconds2);
  }
  
  if(menuCount==8)
  {
    T.Timer();
//substract Show Stop Time
  }
  if(menuCount==9)
  {
      ON_LT3760=HIGH;
      T2.Timer();
      digitalWrite(14,ON_LT3760);//Switch on the Device during T2 State.
      PWMcontrol();//allow PWM Control
      Serial.println(" Run T2 ");
    
  }
     if(menuCount==10)
  {
     
        //loop Timer 1
        Serial.print("condition T2 has ended, Back To Timer 1 which Menu = ");
        Serial.print(menuCount);
    }
  
 
  
}

  int Menu2Interval() //Interval Timer
{
 
  if (clk.update()) 
  {
   Serial.println(" I am in the Menu2interval");
   
    if(clk.fallingEdge())
    {
      Serial.print(" I ");
      Serial.println(IntCount);
      IntCount=IntCount+1; //move statement after pressed
      icount++;
      icountAt=millis();
    }
      
  }else
    {
      if(icount!=icountPrinted)
      {
        unsigned long nowMillis=millis();
        if(nowMillis - icountAt>20)  //shortened the bounce interval to increase the speed of the button
        {
         icountPrinted=icount;
        }
      }
        //do nothing, 
      //     Serial.print("Button not Pressed ");
    }

  
  
  if (IntCount>3)  //limit the menu
  {
    IntCount = 1;
    
  }

  if (IntCount == 1) 
  {
      OFFTime1=10*TimeControlEncoder();      
      
  }
  else OFFTime1=0;
  
  if(IntCount==2)
  {
    
      ON_Time1=10*TimeControlEncoder();
      ON_LT3760==HIGH;
    }
    else  ON_Time1=0; //out of boundary set to zero.
   
 
if(IntCount==3)
  {
  
  
  unsigned long currentMillis=millis();
  if((ON_LT3760==HIGH)&&(currentMillis-PreviousMillis>=ON_Time1))
  {
    ON_LT3760=LOW;
    PreviousMillis=currentMillis;
    PWMcontrol();
    //some LED for indication or on the display showing Interval 1
  }
  else if((ON_LT3760==LOW)&&(currentMillis-PreviousMillis>=OFFTime1))
 {
  ON_LT3760=HIGH;
  PWMcontrol();
  PreviousMillis=currentMillis;
  
 }
  
  
  
  
  }
}

int Menu1Countdown() //Menu 1
{
 /* all graphics commands have to appear within the loop body. */    
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_helvB08_tr);
    u8g2.setFontMode(0);  /* activate transparent font mode */
    u8g2.setFontDirection(0);
    
    u8g2.drawStr(0,45,"Set T1");
    u8g2.setCursor(0,60);
    u8g2.print(Hours,DEC);
    u8g2.print(":");
    u8g2.print(Minutes,DEC);
    u8g2.print(":");
    u8g2.print(Seconds,DEC);
    u8g2.drawStr(0,80,"Set T2");
    u8g2.setCursor(0,95);
    u8g2.print(Hours2,DEC);
    u8g2.print(":");
    u8g2.print(Minutes2,DEC);
    u8g2.print(":");
    u8g2.print(Seconds2,DEC);
 
   if (menuCount > 7 && menuCount == 8) 
   {
     if(T.TimeHasChanged())
     { 
      Serial.println(" R T1 ");      
      u8g2.drawStr(60,10,"RUN T1");
     
      u8g2.setCursor(60,60);
      u8g2.print(T.ShowHours(),DEC);
      u8g2.print(":");
      u8g2.print(T.ShowMinutes(),DEC);
      u8g2.print(":");
      u8g2.print(T.ShowSeconds(),DEC);
  
      Serial.print(T.ShowHours());
      Serial.print(":");
      Serial.print(T.ShowMinutes());
      Serial.print(":");
      Serial.print(T.ShowSeconds());
      
      }
       else 
       {
       u8g2.drawStr(60,10," S T1 ");
       menuCount=9;
       }
    
  }
    
 if(menuCount>8&&menuCount==9)
 {
  
      if(T2.TimeHasChanged())
     { 
      Serial.println(" R T2 ");      
      u8g2.drawStr(60,10,"RUN T2");
      
      u8g2.setCursor(60,95);
      u8g2.print(T2.ShowHours(),DEC);
      u8g2.print(":");
      u8g2.print(T2.ShowMinutes(),DEC);
      u8g2.print(":");
      u8g2.print(T2.ShowSeconds(),DEC);
  
      Serial.print(T2.ShowHours());
      Serial.print(":");
      Serial.print(T2.ShowMinutes());
      Serial.print(":");
      Serial.println(T2.ShowSeconds());
      
      }
       else 
       {
        u8g2.drawStr(60,10,"S T2");
       }
    }
    if(menuCount>9&&menuCount==10)
    {
      u8g2.drawStr(60,10,"RESTART COUNTDOWN");
       menuCount=1;
    }
     
   
  
  u8g2.drawStr(5,10,"M");
  u8g2.setCursor(15,10 ); //just a symbol to show where I am in the Menu
  u8g2.println(menuCount);
  u8g2.display();
  u8g2.sendBuffer();
  }




int Menu2IntervalDisplay()
{
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_helvB08_tr);
    u8g2.setFontMode(0);  /* activate transparent font mode */
    u8g2.setFontDirection(0);
    
    u8g2.drawStr(0,40,"PWM %");
    u8g2.drawRFrame(45,29,90,10,0);// x,y,w,h,_round
    int Intensity=PWMControlEncoder();
    u8g2.drawBox(45,30,Intensity,8);
    u8g2.drawStr(0,50,"OnTime ms");
    u8g2.setCursor(60,50);
    u8g2.print(": ");
    u8g2.print(ON_Time1,DEC);
    u8g2.drawStr(0,65,"OFFTime ms ");
    u8g2.setCursor(60,65);
    u8g2.print(": ");
    u8g2.print(OFFTime1,DEC);

    if(IntCount>0&&IntCount==1)
    {
        u8g2.drawBox(80,50,10,10);
    }

    
    if(IntCount>1&&IntCount==2)
    {
     
        u8g2.drawBox(80,65,10,10);
    }

  u8g2.drawStr(5,10,"I");
  u8g2.setCursor(15,10 ); //just a symbol to show where I am in the Menu
  u8g2.println(IntCount);
  u8g2.display();
  u8g2.sendBuffer();
  }




 






  //menu 1 for CountDown Timer


void setup() {



T.StartTimer();
T2.StartTimer(); 




analogWriteFrequency(Pin_25_OUT_PWM,200);

Serial.begin(115200);

pinMode(CountDownPin, INPUT);
pinMode(IntervalPin, INPUT);
pinMode(ExternPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
  
analogWriteResolution(12);
analogReadResolution(12);
  
  pinMode(20, INPUT);
  pinMode(14,OUTPUT);  //SHDN Not Pin, switch on the LT3760

analogWriteResolution(12);
analogWriteFrequency(25,200);
analogReadResolution(12);

  u8g2.setBusClock(24000000);
  u8g2.begin();
}


void loop() 
{ 

  
  newSwitchState1=digitalRead(CountDownPin);
  if(newSwitchState1!=OldSwitchState1)
  {
    if(newSwitchState1==HIGH)
    {
       StateInput=1;
    }
 }
 
  newSwitchState2=digitalRead(IntervalPin);
  if(newSwitchState2!=OldSwitchState2)
  {
    if(newSwitchState2==HIGH)
    {
        StateInput=2; 
    }
  }
  
  newSwitchState3=digitalRead(ExternPin);
  if(newSwitchState3!=OldSwitchState3)
  {
   if(newSwitchState3==HIGH)
   {
        StateInput=3; 
   }
  }

switch(StateInput)
  {
    
    case 1:
    {
       //Serial.println("CountDown is ON");
    Menu2Interval();
    Menu2IntervalDisplay(); 
    u8g2.display();
    u8g2.sendBuffer();
   
    
    break;
   
    
    }
case 2:
    {
     
//    Serial.println("EXTN is ON");

    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_helvB08_tr);
    u8g2.setFontMode(0);  /* activate transparent font mode */
    u8g2.setFontDirection(0);
    u8g2.drawStr(20,0,"PWM CONTROL");
    u8g2.drawRFrame(35,29,90,10,0);// x,y,w,h,_round
    u8g2.drawStr(0,40,"%"); //x,y,String
    int Intensity=PWMControlEncoder();
    u8g2.drawBox(35,30,Intensity,8);


    
    ON_LT3760=HIGH;
    digitalWrite(14,ON_LT3760);
    PWMcontrol();
    break;
   
    }
case 3:
    {
      // Serial.println("Interval is ON");
    Menu1Check();
    Menu1Countdown();
    
   break;
    }
  }

}
 
Last edited by a moderator:
Status
Not open for further replies.
Back
Top