mysterious problem at startup

I have what I would say is a fairly simple sketch. I am using some of the code from DG21 demo specifically code for the menu system.

In my sketch I am reading some thermistors.Each approx 5 seconds I read the sensors using the steinhart equation process. I sample each one 20 times with a 20 millisecond delay between the samples then average them. As you can see that process takes some time.

In an effort to use the buttons and the menu functions seamlessly  think I have to hold up the thermistor reading process then do my button stuff and then go back to running the processing part of my sketch.

So I have a variable that I call 'runMode' that I set to 1 at startup so that the thermistor reading and processing part of the sketch runs. When the up arrow is pressed I am setting the 'runMode' variable to 0 so that only the button process is running.

The problem is that upon startup the 'runMode' variable is getting changed to 0 even though no buttons are pressed. Furthermore none of the other lines of code where I set the variable to 0 are getting executed.

Here is my void loop()

void loop()
{
  ReadButtons(); //check buttons
  Navigate(); //update menus and perform actions
   
  if ((millis() - lOutputLoop) > 5000)
  {
    lOutputLoop = millis();
     SerialUSB.print(runMode);
    if (runMode)
    {
      twoToTenOut();
      twoToTenOutPrint();
    }
    else
    {
      Indio.analogWrite( 2, 0, true );                // Set channel 2 to bypass
    }
  }
}

Here is the code from the ReadButtuns function slightly altered from the demo.

void ReadButtons()
{
  buttonEnterState = digitalRead(buttonEnterPin);
  buttonUpState = digitalRead(buttonUpPin);
  buttonDownState = digitalRead(buttonDownPin);

  if (buttonEnterState == HIGH && prevBtnEnt == LOW)
  {
    if ((millis() - lastBtnEnt) > transEntInt)
    {
      enterPressed = 1;
    }
    lastBtnEnt = millis();
    lastAdminActionTime = millis();
    SerialUSB.print("Enter Pressed ");
    SerialUSB.println(enterPressed);
  }
  prevBtnEnt = buttonEnterState;

  if (buttonUpState == HIGH && prevBtnUp == LOW && startOK)
  {
    if ((millis() - lastBtnUp) > transInt)
    {
      channel--;
    }
    lastBtnUp = millis();
    lastAdminActionTime = millis();
    SerialUSB.println("UpPressed");

    if (runMode)
    {
      SerialUSB.println("runMode to 0");
      runMode = 0;
      MenuMain();
    }
  }
  prevBtnUp = buttonUpState;

  if (buttonDownState == HIGH && prevBtnDown == LOW)
  {
    if ((millis() - lastBtnDown) > transInt)
    {
      channel++;
    }
    lastBtnDown = millis();
    lastAdminActionTime = millis();
    SerialUSB.println("DownPressed");
  }
  prevBtnDown = buttonDownState;

  if (constrainEnc == 1)
  {
    channel = constrain(channel, channelLowLimit, channelUpLimit);
  }
}

On startup the only thing that is happening is runMode is getting set to 0. This is the only place in the sketch where I set it to 0.  When I comment out the line the variable stays as a 1. The serialUSB.print is not firing, neither is the MainMenu() action.

As you may notice I have added another variable 'startOK' which is initially set to 0 then after the first run of my twoToTen function I set it to 1. This seems to be a work around that is working but I am still concerned how the runMode variable is getting changed to 0 when that 'if' control structure should not fire.

How is that happening?

Dries Van Craen
Dries Van Craen
54
| 0 0 0
Asked on 5/24/21, 10:28 AM
0
vote
1037 Views

Hi, it's difficult to troubleshoot partial code, we can only guess how you declare the variables and configure the i/o.

Often the reason for unexpected behaviour is the use of Serial somewhere instead of SerialUSB.

If i understand correctly what you're trying to do, i would suggest attaching interrupts to the buttons, pointing to 1 ISR to set a flag to escape from your reading function, see https://github.com/Industruino/documentation/blob/master/indio.md#interrupts

Tom
Tom
5675
| 1 1 3
Answered on 5/24/21, 11:49 PM
0
vote

I do not have a Serial somewhere instead of SerialUSB.anywhere in my code so I am still not sure of the cause. Thanks for the link to the interupts. That was my first thought but I was not able to find information about it. That is the way I am going to go. Thanks again.

Dries Van Craen
on 5/25/21, 12:59 PM

I have found that I have a couple of lines with analogWrite instead of Indio.analogWrite maybe that is it. I will change it later just to see.

Dries Van Craen
on 5/25/21, 1:06 PM

FYI I copied these lines from the demo code. analogWrite(backlightPin, (map(backlightIntensity, 5, 1, 255, 0))); //convert backlight intesity from a value of 0-5 to a value of 0-255 for PWM.

Dries Van Craen
on 5/25/21, 1:21 PM

OK your suggestion on using interrupts works perfectly. Thank you so much for the help. Here is my revised code. void setup() { Indio.analogWriteMode( 1, V10_p ); // 5000) { lOutputLoop = millis(); twoToTenOut(); twoToTenOutPrint(); } } else { if ((millis() - lOutputLoop) > 20000) { runMode = 1; lcd.clear(); } } } void ReadButtons() { buttonEnterState = digitalRead(buttonEnterPin); buttonUpState = digitalRead(buttonUpPin); buttonDownState = digitalRead(buttonDownPin); if (buttonEnterState == HIGH && prevBtnEnt == LOW) { if ((millis() - lastBtnEnt) > transEntInt) { enterPressed = 1; } lastBtnEnt = millis(); lastAdminActionTime = millis(); SerialUSB.print("Enter Pressed "); SerialUSB.println(enterPressed); } prevBtnEnt = buttonEnterState; if (buttonUpState == HIGH && prevBtnUp == LOW) { if ((millis() - lastBtnUp) > transInt) { channel--; } lastBtnUp = millis(); lastAdminActionTime = millis(); SerialUSB.println("UpPressed"); prevBtnUp = buttonUpState; if (buttonDownState == HIGH && prevBtnDown == LOW) { if ((millis() - lastBtnDown) > transInt) { channel++; } lastBtnDown = millis(); lastAdminActionTime = millis(); SerialUSB.println("DownPressed"); } prevBtnDown = buttonDownState; if (constrainEnc == 1) { channel = constrain(channel, channelLowLimit, channelUpLimit); } }

Dries Van Craen
on 5/26/21, 3:19 PM

Sorry with the formatting of the last comment it seems that i do not have enough 'karma' to format it properly.

Dries Van Craen
on 5/26/21, 3:22 PM

.

Dries Van Craen
Dries Van Craen
54
| 0 0 0
Answered on 5/25/21, 12:58 PM
0
vote

Obviously the Indo.analogWrite was not the problem since that throws an error.

Dries Van Craen
on 5/26/21, 1:21 PM

Your answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

Ask a Question

Keep Informed

About This Forum

This community is for professionals and enthusiasts of our products and services.

Read Guidelines

Question tools

63 follower(s)

Stats

Asked: 5/24/21, 10:28 AM
Seen: 1037 times
Last updated: 5/25/21, 12:59 PM