Change settings using pushbutton

I am trying to do the following but I am having some issues, hoping that someone has a snip of code to show how this will work.

I have a pushbutton, when I push the button down one time I want to be able to change the values on my rotary encoder (inc number), when I push the button a second time after making changes I want to be able to lock the changes.

I was messing around with a sketch to see if i could figure it out but so far nothing is working right. I noticed that I am only able to detect the push button in the setup() area of the sketch, then when I make changes and look for the second press in the loop() section the program doesn't seem to go past that check.

Ar first I had all the checks in the loop() but that didn't work. What makes it difficult for me about coding for arduino is there is no visual debugger, cant trace the code. I have to rely on Serial.print() in order to see whats happening (haven't gotten used to that yet).

If anyone has a snip they can post to get me started the right way instead of me stumbling through and butchering what I am doing would be very helpful.

Thanks

Yes we have no snippets. What we do have is an abundance of advice on how to correct YOUR snippet. So, post your code and let's see where it's not quite right.

If anyone has a snip they can post to get me started the right way instead of me stumbling through and butchering what I am doing would be very helpful.

Without knowing HOW you are trying to do what you give a snip explanation of there is no guessing at what you need.

Right off the bat I'd say that a belief in fix-snippets is a great way to keep stumbling along.

I will try my best but remember this is very very new for me. I am just reading and piecing code together to get it working so I can expand from there. The goal is to have it work like below.

  1. When the arduino boots up my 7 segment display will display all zeros.
  2. Press the push button on the rotary to allow editing.
  3. Once the button is pressed I can count up or down on the display one millisecond at a time.
  4. Press the rotary again to lock the numbers and not allow any more changes unless I press the button again (one press to start editing, second press to lock changes).

dont laugh at my butchered code.

const int rotary_CLK = 3;
const int rotary_DT = 2;
const int rotary_SW = 0;   // rotary push button

int rotary_state = 0;
int rotary_counter = 0;
volatile int current_time = 0;
unsigned long prev = 0;
unsigned long waitmilli = 0;
int time_counter = 0;
unsigned long start = millis();
char display[4];
int disp_count[4];
int display_counter = 0;
const int SCLK_pin = 10;
const int RCLK_pin = 11;
const int DIO_pin = 12;


void rotary_interrupts()
{
  if(rotary_counter == 1)  // no press no change
  {
        Serial.print("rotary_counter interrupt(): ");
        Serial.println(rotary_counter);
  static unsigned long last_time = 0;
  unsigned long current_time = millis();

  if (current_time - last_time > 5)
  {
    if (digitalRead(rotary_DT) == LOW)
    {
      current_time = current_time + 1;
    }
    else
    {
      current_time = current_time - 1;
    }

    current_time = min(1000, max(0, current_time));
    last_time = current_time;
  }
}

void showDisplay()
{
  for (int i = 0; i < 4; i++)
  {
    int val = disp_c[i];
    if ((val >= 32) && (val <= 47))
    {
      switch (val)
      {
        case 45 : val = 20;
          break;
        default : val = 20;
          break;
      }
    }
    else if ((val >= 48) && (val <= 57)) //0-9
    {
      val -= 48;
    }
    disp_count[i] = val;
  }
}
void showCounter()
{
  showDisplay();
  for (int i = 0; i < 4; i++)
  {
    setDigits(i, disp_count[i]);
  }
}
void setDigits(int dig, int character)
{
  int digits[] = {128, 64, 32, 16, 8, 4, 2, 1};

  int characters[] = {3, 159, 37, 13, 153, 73, 65, 31, 1, 9, 2, 158, 36, 12, 152, 72, 64, 30, 0, 8, 255};

  digitalWrite(RCLK_pin, LOW);
  shiftOut(DIO_pin, SCLK_pin, LSBFIRST, characters[character]);
  shiftOut(DIO_pin, SCLK_pin, LSBFIRST, digits[dig]);
  digitalWrite(RCLK_pin, HIGH);
}
void showText(char a, char b , char c, char d)
{
  display[0] = d;
  display[1] = c;
  display[2] = b;
  display[3] = a;
}

void setup()
{
  Serial.begin(9600);
  pinMode(rotary_CLK, INPUT);
  pinMode(rotary_DT, INPUT);
  pinMode(rotary_SW, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(rotary_CLK), rotary_interrupts, LOW);

  if(rotary_state == LOW)
    {     
      Serial.print("rotary_state setup(): ");
      Serial.println(rotary_state);
      if(rotary_counter >= 2) rotary_counter = 0;  // reset the counter back to zero
      rotary_counter++;
      delay(1000);
    }
}


void loop() 
{
showCounter();	

if(Rotary_Push_Button_Counter == 2)
	{
	rotary_counter = 0;
        Serial.print("rotary_counter loop(): ");
        Serial.println(rotary_counter);

        if( millis() > (prev + waitmilli))
        { 
	showText(' ', (time_counter / 1000) % 10, (time_counter / 100) % 10 + 10, (time_counter / 10) % 10);
	display_counter--;
	}
}

Appreciate any help, its always good to learn proper coding techniques so this is my start.

Thanks

I might be missing something, but I don't see anywhere where you actually read the state of the rotary switch pin.

PaulS:
I might be missing something, but I don't see anywhere where you actually read the state of the rotary switch pin.

I have it all in there now. Hopefully I can get a more efficient way of doing this as there are some things like flickering that I see that i dont like.

Thanks.

PaulS:
I might be missing something, but I don't see anywhere where you actually read the state of the rotary switch pin.

Me neither and I triple-checked.

I hope you're well Pauil, and just trying to be nice... does it hurt? Let's see iof this part helps,

After more time spent reading the code, this routine looks like a joke.

void rotary_interrupts()
{
  if(rotary_counter == 1)  // no press no change
  {
        Serial.print("rotary_counter interrupt(): ");
        Serial.println(rotary_counter);
  static unsigned long last_time = 0;
  unsigned long current_time = millis();

  if (current_time - last_time > 5)
  {
    if (digitalRead(rotary_DT) == LOW)
    {
      current_time = current_time + 1;
    }
    else
    {
      current_time = current_time - 1;
    }

    current_time = min(1000, max(0, current_time));
    last_time = current_time;
  }
}

You do NOTHING with the adjusted current_time and every time the interrupt occurs you set it to millis() anyway.

I see a lot of unconnected code. It looks like someone threw the parts to a machine into a box and expect to shake it till it all falls together. Eventually you might get it to run but mixing a bunch of unknowns together is how you make a debug nightmare.

You're wanting to do something but the story this code tells is like a transcript of a Trump speech, meaningless garbage.
I only tell this because I want the OP to quit wasting his/her own time and find a better way to code.

void loop() 
{
showCounter();	

if(Rotary_Push_Button_Counter == 2)

When does variable “Rotary_Push_Button_Counter” ever get set / incremented to make == 2 a possibility?

Agree with GoForSmoke... the code doesn't tell a clear story. Just a suggestion, you may want to write out at a high level the routines you think need to be done. As an example:
-ID Libraries
-create variables
-Setup initial states
-Loop Routines
-routine #1 (Enter configuration state #1 - 1st button press)
-Subroutine #1a (change number)
-routine #2 (Enter configuration state #2 - 2nd button press)
-subroutine #2a (change number)

etc.

This will help you tell the story at a concept level and much easier to add code to accomplish each thing.

Just a thought.

You guys can look at the code and tell that it doesn't tell the story. If I knew what I was doing to make the code work I truly wouldn't be here. From what I have noticed there are a lot of rookies in here asking for guidance because they have no clue what they are doing. I do remember stating that this is what I was trying to accomplish but really didnt know how to accomplish it, this is the reason I asked. My code is butchered because I am trying to get something working.

It seems like in every programming forum every one is an expert but yet they hang out in the forums telling the beginners their code is garbage. Sorry to waste you guys time, guess I will keep browsing the web and hopefully find some snippets that work and put it together to come up with what I need.

I really wish all of us in here were experts, then none of us would be hanging out in here.

stevieb12:
I have to rely on Serial.print() in order to see whats happening (haven't gotten used to that yet).

Yes, Serial.print can be clunky but, putting it in your ISR is a no-no. The ISR should be a minimal routine - set a flag or value or two and get out.

If you haven't already, I suggest trimming down to just getting the encoder to reliably drive a counter up and down. Build on that. Check out some encoder libraries to get an idea of how it's done.

stevieb12:
You guys can look at the code and tell that it doesn't tell the story. If I knew what I was doing to make the code work I truly wouldn't be here. From what I have noticed there are a lot of rookies in here asking for guidance because they have no clue what they are doing. I do remember stating that this is what I was trying to accomplish but really didnt know how to accomplish it, this is the reason I asked. My code is butchered because I am trying to get something working.

It seems like in every programming forum every one is an expert but yet they hang out in the forums telling the beginners their code is garbage. Sorry to waste you guys time, guess I will keep browsing the web and hopefully find some snippets that work and put it together to come up with what I need.

I really wish all of us in here were experts, then none of us would be hanging out in here.

This is a Forum to help people with code they've written. Many mistakenly come here thinking that we're going to write code for them or teach them how. There is a Gigs and Collaborations for the former and a Tutorials forum for the latter. So sorry you ended up in the wrong forum, that's not our fault.

DKWatson:
This is a Forum to help people with code they've written. Many mistakenly come here thinking that we're going to write code for them or teach them how. There is a Gigs and Collaborations for the former and a Tutorials forum for the latter. So sorry you ended up in the wrong forum, that's not our fault.

Looks like you are defending the actions of folks that have no home life and the keyboard and forum is the only way they feel manly. I dont remember asking any political questions in my post, yet I get a moronic answer. A few people here responded kindly which I respect. The best answer would have been you are in the wrong forum, go over to that specific forum and they will guide you.

I have been reading quite a few posts from others that are totally confused, you can tell they are just learning by the questions they are asking. To my surprise when I saw some of the RUDE answers they were given, vey uneducated people I must say.

But hey, this is the state of the country today, all the wrong we do just accept it. Now isn't that the american way!!!

dougp:
Yes, Serial.print can be clunky but, putting it in your ISR is a no-no. The ISR should be a minimal routine - set a flag or value or two and get out.

If you haven't already, I suggest trimming down to just getting the encoder to reliably drive a counter up and down. Build on that. Check out some encoder libraries to get an idea of how it's done.

Appreciate it dougp, I started going in that direction earlier. I started looking at how the encoder works, then I can move onto the segment display and combine them together. I also saw what you mentioned about putting stuff in the ISR, I definitely have a ton to learn about working with Arduino code.

Appreciate the info...

In the time you spent bashing the forum that actually gave you lots of advice, you could have had a V8...

Definately use isr for encoders, but don’t run code in a isr.. set a variable and get out.

stevieb12:
You guys can look at the code and tell that it doesn't tell the story. If I knew what I was doing to make the code work I truly wouldn't be here. From what I have noticed there are a lot of rookies in here asking for guidance because they have no clue what they are doing. I do remember stating that this is what I was trying to accomplish but really didnt know how to accomplish it, this is the reason I asked. My code is butchered because I am trying to get something working.

It seems like in every programming forum every one is an expert but yet they hang out in the forums telling the beginners their code is garbage. Sorry to waste you guys time, guess I will keep browsing the web and hopefully find some snippets that work and put it together to come up with what I need.

I really wish all of us in here were experts, then none of us would be hanging out in here.

Sorry Stevie. I did write code for money for 19 years and did go through my own teething process of which I try to save others the worst of which can take years to get past.

I keep telling telling people to start small and develop in pieces. The last thing you want to assemble a pile of code you aren't sure of before debugging any of it.

See what you can find on troubleshooting, especially about reducing unknown elements.