Help with this sketch and its library

Pleas help. Attached is my sketch .h and .cpp library. Why does it not recognise the argument ‘ton’?


MySketch2.cpp (1.04 KB)

Please POST your code

Hang on I’ll try again.

// MySketch2.cpp 

#include "Arduino.h"

#include "MySketch2.h" 

MySketch2::MySketch2(int pinOne, int pinTwo, int ton){

 pinMode(pinOne, INPUT);
 pinMode(pinTwo, OUTPUT);


 _pinOne = pinOne;
 _pinTwo = pinTwo;
}

void MySketch2::period(){
 
     duration = pulseIn(_pinOne,HIGH);  
 mSecs = duration/1000;
 Serial.println(mSecs);
 sigout();  
 
}

void MySketch2::sigout(){
 
    int ton;
 
 if(mSecs >= ton & mSecs <= ton)  
    {
     Serial.println(" Valid pulseIn rec'd");
     digitalWrite(_pinTwo, HIGH); 
     delay(800); 
     digitalWrite(_pinTwo, LOW);
       delay(400);  
 }
   
 // utilising int ton is pretty much ignored by the sketch
 // prints  "Valid ... " regardless of value used in sketch

}
#ifndef MySketch2_h
                                  
#define MySketch2_h

#include "Arduino.h"

class MySketch2{

 public:
 
 MySketch2(int pinOne, int pinTwo, int ton);
 
 void period();              
 void sigout();

 private:                  
 int mSecs;
    unsigned long duration;
 int _pinOne, _pinTwo;
};

#endif
 // Saved as MySketch2.ino 6/5/2020

#include <MySketch2.h>
 MySketch2 Capture = MySketch2(2,11,30); 
 


void setup() {
 Serial.begin(9600);
 Serial.println(" MySketch2-01.ino 6/5/20 ");
}
void loop() {
    Capture.period();
 ! 
}

You can edit posts.
Click on the “more” control on the bottom right, then “modify”

I am very confused as to what code you are trying to compile
Please post the .ino, .h and .cpp files, each in code tags making it clear which is which

I am confused too. I did use code tags for each file .ino .h and .cpp all done sequentially and appear in my last reply.

You can edit posts.
Click on the “more” control on the bottom right, then “modify”

Have edited the three files .ino .h and .cpp removing comments for clarity. Hope this works for you.

‘ton’ has no value, so is undefined.

Generally, don’t put hardware stuff like pinMode in a constructor, put it in a begin method.

You never assign the constructor's argument 'ton' to a class variable.

Please help further by example? Where do I put ton
Thanks in advance

Where do I put ton

Assign it to a class variable in the same way that pinOne and pinTwo are assigned to a class variable in the constructor (see below) then use the class variable in the class when you need it

MySketch2::MySketch2(int pinOne, int pinTwo, int ton){

 pinMode(pinOne, INPUT);
 pinMode(pinTwo, OUTPUT);


 _pinOne = pinOne;
 _pinTwo = pinTwo;
}

Thanks for you advice. However I believe I did put ton in the constructor? See my original post of that code? Is the problem in .cpp OR sketch?
Mike

You pass three arguments to the constructor. You handle 2 of them correctly. Why did you treat 'ton' differently?

gfvalvo:
You pass three arguments to the constructor. You handle 2 of them correctly. Why did you treat 'ton' differently?

I'll check that out see if I can do same for ton as for others. I have for sure followed the method, tried and tried many ways, as for pinOne and pinTwo for ton but could not get it right, hence my reason for being on this forum. Could you demonstrate by modifying my codes for me to see exactly what I should have done, please?
Mike

Your evidence has made do exactly same with ton as I did for pinOne and pinTwo! Works fine now, the thing you noticed was in fact very relevant ... _ton was the solution.
Good for now and thanks a lot Mike

I assume that you missed reply #11