Tester for analogic timers, not buttonstate.

Hi, i have been looking around for a way to do the following problem:

I´m trying to develop a probe tester for my analogic timers such as the 555 or HEF4020. The whole idea is to to get one message when the Pin is High and one when the Pin is Low so i can keep it recording for some hours and then compile the data and analyze if there is deviance. So i´m using a RTC to keep precise time and simplifying my code. Also the idea is to use more Pins in the future.

So, i want a code that helps me read when a Pin is HIGH and then uses this information to write a single message in my serial port. And when the Pin is LOW it will return a single message to my serial port. For instance i have T1 (Low) and T2(High): T1 is 25min T2 is 2min.

Until now all the code i have seen about this is related to Button Switching, and that´s what my code uses today with the problem of huge quantity of data that's hard to analyze.

Can someone point me in the right direction.

Thank you all in advance.

Reading a pin HIGH or LOW from any other device is no different from reading a pin going HIGH or LOW from a button. I guess I don't understand your confusion.

Is your problem that it keeps spitting out the message over and over as long as the pin is in one state and you want it to spit the message out only once when the pin changes state? If that's the case, look at the "State Change" example that comes with the IDE for some inspiration. Again, it is related to a button but there's nothing there that requires it to be a button. The HIGH and LOW signals could be coming from anything and the same process works.

It sounds like you want to detect when a pin becomes HIGH or LOW rather than when it is HIGH or LOW. Look at the StateChangeDetection example in the IDE to see how to do it.

Delta and UKHeliBob you were right. I made a code with state detection and it worked. I just had to invert the High Low for the sake of my outputs as i’m using an transistor to convert the 12V input in 5V.

Here is the code:

const int  buttonPin = 13;    

#include <virtuabotixRTC.h>
// myRTC(clock, data, rst)
virtuabotixRTC myRTC(10, 12, 11);


int buttonPushCounter = 0;   
int buttonState = 0;         
int lastButtonState = 0;     

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  
  digitalWrite(buttonPin, HIGH); 
  // pull up resistor.
  Serial.begin(9600);
  //RTC
  myRTC.setDS1302Time(59, 10, 15, 3, 3, 11, 2015);
}


void loop() {
  
    myRTC.updateTime();
    
 
  buttonState = digitalRead(buttonPin);

  
  if (buttonState != lastButtonState) {
  
    if (buttonState == HIGH) {
      
      Serial.print("OFF  ");
    Serial.print(myRTC.dayofmonth);
  Serial.print("/");
  Serial.print(myRTC.month);
  Serial.print("/");
  Serial.print(myRTC.year);
  Serial.print(" ");
  if (myRTC.hours < 10)
  {
    Serial.print("0");
  }
  Serial.print(myRTC.hours);
  Serial.print(":");

  if (myRTC.minutes < 10)
  {
    Serial.print("0");
  }
  Serial.print(myRTC.minutes);
  Serial.print(":");

  if (myRTC.seconds < 10)
  {
    Serial.print("0");
  }
  Serial.println(myRTC.seconds);
    } else {
    Serial.print("ON ");
    Serial.print(myRTC.dayofmonth);
  Serial.print("/");
  Serial.print(myRTC.month);
  Serial.print("/");
  Serial.print(myRTC.year);
  Serial.print(" ");
  if (myRTC.hours < 10)
  {
    Serial.print("0");
  }
  Serial.print(myRTC.hours);
  Serial.print(":");
   if (myRTC.minutes < 10)
  {
    Serial.print("0");
  }
  Serial.print(myRTC.minutes);
  Serial.print(":");
    if (myRTC.seconds < 10)
  {
    Serial.print("0");
  }
  Serial.println(myRTC.seconds);
    }
     delay(50);
  }
    lastButtonState = buttonState;

}

Thank you very much.

:smiley:

How about using a function to output the date and time data to avoid repeating code and another one to print a "0" when passed a value that is less than 10 ?

Well, i thought to myself: build you own code instead of copying and pasting:

const int pin13 = 13;
const int pin7 = 7;

//include rct latter

int pinstate1 = 0;
int lastpinstate1 = 0;

int pinstate2 = 0;
int lastpinstate2 = 0;


void setup() {

 pinMode(pin13, INPUT);
 pinMode(pin7, INPUT);

 digitalWrite(pin13, HIGH);
 digitalWrite(pin7, HIGH);

 Serial.begin(9600);


 //For pullup resistor

//RTC LATER
}

void printondate() {
  Serial.print("ON ");
}

void printoffdate() {
  Serial.print("OFF ");
}

void loop() {
  
pinstate1 = digitalRead(pin13);
pinstate2 = digitalRead(pin7);

if (pinstate1 != lastpinstate1) {

  if (pinstate1 == HIGH) {

     Serial.print("ON ");
  
  }    else
    Serial.print("OFF ");
    delay(100);
}
     pinstate1 = lastpinstate1;

if (pinstate2 != lastpinstate2) {

  if (pinstate2 == HIGH) {
    
      Serial.print("ON ");
  
  }    else
    Serial.print("OFF ");
     delay(100);
       
       pinstate2 = lastpinstate2;
}
}

But now it does not work.

My output in serial if flickering ON. And with the old code it works.

Any idea? I'm really just getting into code.

Just realized i have inverted the laststate with currentstate. Here is the code:

#include <virtuabotixRTC.h>
// myRTC(clock, data, rst)
virtuabotixRTC myRTC(10, 12, 11);

const int pin13 = 13;
const int pin7 = 7;

//include rct latter

int pinstate1 = 0;
int lastpinstate1 = 0;

int pinstate2 = 0;
int lastpinstate2 = 0;


void setup() {

 pinMode(pin13, INPUT);
 pinMode(pin7, INPUT);

 digitalWrite(pin13, HIGH);
 digitalWrite(pin7, HIGH);

 Serial.begin(9600);

 
 //For pullup resistor

//RTC LATER
}

void printondate() 
{
  Serial.print("ON  ");
  Serial.print(myRTC.dayofmonth);
  Serial.print("/");
  Serial.print(myRTC.month);
  Serial.print("/");
  Serial.print(myRTC.year);
  Serial.print(" ");
  if (myRTC.hours < 10)
  {
    Serial.print("0");
  }
  Serial.print(myRTC.hours);
  Serial.print(":");

  if (myRTC.minutes < 10)
  {
    Serial.print("0");
  }
  Serial.print(myRTC.minutes);
  Serial.print(":");
    if (myRTC.seconds < 10)
  {
    Serial.print("0");
  }
  Serial.println(myRTC.seconds);
}

void printoffdate() {
  Serial.print("OFF  ");
  Serial.print(myRTC.dayofmonth);
  Serial.print("/");
  Serial.print(myRTC.month);
  Serial.print("/");
  Serial.print(myRTC.year);
  Serial.print(" ");
  if (myRTC.hours < 10)
  {
    Serial.print("0");
  }
  Serial.print(myRTC.hours);
  Serial.print(":");

  if (myRTC.minutes < 10)
  {
    Serial.print("0");
  }
  Serial.print(myRTC.minutes);
  Serial.print(":");
    if (myRTC.seconds < 10)
  {
    Serial.print("0");
  }
  Serial.println(myRTC.seconds);
}

void loop() {

myRTC.updateTime();
  
pinstate1 = digitalRead(pin13);
pinstate2 = digitalRead(pin7);

if (pinstate1 != lastpinstate1) {

  if (pinstate1 == HIGH) {

     Serial.print("DOS1  ");
     printoffdate();
  
  }    else
  
    Serial.print("DOS1  ");
    printondate();
    delay(100);
}
    lastpinstate1 = pinstate1;

if (pinstate2 != lastpinstate2) {

  if (pinstate2 == HIGH) {
    
    Serial.print("DOS2  ");
    printoffdate();
  
  }    else

    Serial.print("DOS2  ");
    printondate();
     delay(100);
       
      lastpinstate2 = pinstate2;
}
}
void printondate()
void printoffdate()

Close, but no cigar ! What I had in mind was a single printDate() function, perhaps with ON/OFF passed as a parameter.