digitalRead timing out in while loop?

Hello,

I'm a relatively new user, but I've tripped across a weird issue that has me stumped!

I'm using a simple while (digitalRead(touchPin) == HIGH) to call a function that turns on a relay for 10seconds. BUT after about 6 or 7 seconds touchPin is driven LOW and the while is exited. I've tried with 2 switches, a proximity and capacitive touch switch.

I hope I'm just missing something small. I've tried it on a Nano, Pro micro and Attiny85 with the same result. Hoping someone has tripped across this behavior?

Thank you,
Trace

Hi, @trace67
Welcome to the forum.

We need to see your complete code.
Please post it in code tags as explained in the above link.

Can you please post a schematic of your project?
Please do not use Fritzy, a hand drawn image will be fine.
Include ALL power supplies, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the </> icon from the ‘reply menu’ to attach the copied sketch.

Yeah, what is that?

#include <Arduino_BuiltIn.h>
#include <Tiny4kOLED.h>

#define touchPin 9  //Physical Pin 9/A9
#define mosfetPin 6 //Physical Pin 6/A7

unsigned long startTime = millis();
unsigned long currentTime = millis();
unsigned long cooktimeout = 10000; //X second timneout on signal to MOSFET

void setup() {
  pinMode(mosfetPin, OUTPUT);
  pinMode(touchPin, INPUT_PULLUP);
  oled.begin();
  oled.clear();
  oled.on();
  oled.switchRenderFrame();
  oled.clear();
  oled.setFont(FONT8X16);
  oled.setCursor(14,0);
  oled.print(F("System Check"));  //Wrap strings in F() to save RAM
  oled.switchFrame();
  delay(3000);
  }

void loop() {
  startTime = millis();
    oled.clear();
    oled.setFont(FONT8X16);
    oled.setCursor(14,0);
    oled.print(F("READY 2 GO")); 
    oled.switchFrame();
  
  while (digitalRead(touchPin) == HIGH) {   
    currentTime=millis();
     heaton();
     }
    digitalWrite(mosfetPin, LOW); 
     }
 
void heaton() {
  
  if (currentTime-startTime<=cooktimeout)
  {
    currentTime=millis();
    digitalWrite(mosfetPin,HIGH);
    digitalRead(touchPin);
    oled.clear();
    oled.setFont(FONT8X16);
    oled.setCursor(14,0);
    oled.print(F("Coil Heating"));
    oled.setCursor(0,2);
    oled.print((currentTime-startTime)/1000);
    oled.setFont(FONT6X8);
    oled.setCursor(17,2);
    oled.print(F("Seconds"));
    oled.setCursor(70,3);
    oled.print(F("TAB-IH"));
    oled.switchFrame();
    delay(100);

  } else
   {
    digitalWrite(mosfetPin,LOW);
    oled.clear();
    oled.setFont(FONT8X16);
    oled.setCursor(14,0);
    oled.print(F("Heater Timeout"));
    oled.switchFrame();
    delay(1000);
  }
}

Tell us what you think this does. :thinking:

It reads pin 9 on the micro.. if there's voltage, it's HIGH, 0 it's LOW?

I just noticed I left that in. I was hoping the digitalRead(touchPin); would stop it from timing out.. but it didn't.

But it doesn’t do anything useful :woozy_face:

The thought process was that maybe giving it a poke would act as a keep alive.

I just tested the sensor and guess what it does after 6-7 seconds? Another sensor does the same thing too. Not sure what it is, but looks like it's an issue with the touch sensors.

Is this a TTP223 capacitance touch switch ?

?

First thing you need to do is toss the nonsense theory that digitalRead is somehow timing out. It absolutely is NOT. There is no hardware or software present that would cause that to happen, so stop looking there. Your problem lies elsewhere.

Yes it is. It's in 00 00 mode. But it just turns off after about 7 seconds. Maybe just a bad batch?

Nice phrasing to a first time user/poster.

I have found the sensor to be the issue.. for all 4 of them.

Welcome to technology. A place where people call things what they are.

No comment

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.