programing

I want to use my Trinket to carry out what must be a simple task, but after hours of research I am unable to find a way. I should point out that I am very much a learner.
I have a clock pendulum which provides short pulses at 3 per second and I want to input these and output a short square wave after each third pulse i.e at 1 second intervals. It cant be that difficult can it? Any help with the sketch would be appreciated.

clockman

Posts: 16
Joined: Fri Nov 23, 2018 11:28 am

Re: programing

How are you sensing the pendulum?

franklin97355

Posts: 21081
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: programing

The pendulum is driven by the coil and circuit from a cheap quartz pendulum clock. It is completely free of the quartz unit. I am picking up pulses from the simple single transistor drive circuit. The frequency is close to 3 per second and I want to use these to produce a 11 second pulse to drive a slave clock unit. Hope I have explained that ok.

clockman

Posts: 16
Joined: Fri Nov 23, 2018 11:28 am

Re: programing

Correction to that. I want to produce ONE second short square waves.

clockman

Posts: 16
Joined: Fri Nov 23, 2018 11:28 am

Re: programing

What part of the programming do you need help with?

franklin97355

Posts: 21081
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: programing

Pretty well all of it! I have roughly understood the structure of a sketch. I would want to input the pulses to a variable? But I cant work out how to add the incoming pulses, count three and then output my one second pulse. As I said, pretty well all of it.
I think that given the basic idea of what the trinket can do, and how to do it, I should be able to sort something.
As you will have gathered, my experience is virtually none. Can you point me in the right direction?
Thanks.

clockman

Posts: 16
Joined: Fri Nov 23, 2018 11:28 am

Re: programing

Have you been able to connect the pulses to the Trinket and count them? You would check the pin you are connected to and, if there is a signal add one to a variable then check the variable to see if it was three. If so you would output your pulse on another pin. Why are you doing this since there are easier ways to get a one-second pulse, just asking?

franklin97355

Posts: 21081
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: programing

I think I can set up input and output pins and a variable, but what I am missing are commands to add pulses to variable and then presumably ' if variable = 3 then output high to pin, short delay to produce square wave then output low'. Continue looping.
Am I making any sense?
I know I could produce 1sec pulses more easily, but the object is to make a clock using a 'quality' pendulum and to drive a slave clock with the 1 sec pulses as a way to monitor the performance of the pendulum. The clock side is no problem, I just need the software side.
PS the input pulses are 2.5 volt about 25 msec as measured on my mini 'scope.

clockman

Posts: 16
Joined: Fri Nov 23, 2018 11:28 am

Re: programing

You probably want to use interrupts so you do not miss incoming pulses while outputting your square wave. https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/ I believe the original Trinket can only do the rising edge interrupt you need on pin 2 (interrupt 0).
I have never worked with a Trinket but this code should get you close

Code: Select all | TOGGLE FULL SIZE
`const byte outPin = 0;const byte interruptPin = 2;volatile byte state = LOW;volatile int count=0;int outPulseDuration = 50;  //pulse output duration millisecondsvoid setup() {  pinMode(outPin, OUTPUT);  pinMode(interruptPin, INPUT);  attachInterrupt(0, inPulse, RISING);}void loop() {  if(state = HIGH) {              //do square wave    state = LOW;    digitalWrite(outPin, HIGH);    delay(outPulseDuration);    digitalWrite(outPin, LOW);      }}void inPulse() {  count++;  if( count > 2) {    state = HIGH;    count = 0;  }}`

Fellow hobbyist
Keith

kcl1s

Posts: 1247
Joined: Tue Aug 30, 2016 12:06 pm

Re: programing

Thanks for input Keith
INTERUPTS(!!) Things are getting even more involved. You make a good point, but thinking of the timing, there is approx 0.3 sec between input pulses so if I limit the output square wave to say 0.25 sec will that give enough recovery time for the next incoming pulse?
I think I am going to have to dive in and compose a sketch, see if it will compile, and learn by my mistakes.
Do you think I am along the right lines to use a variable to count the incoming pulses, and if so the missing link is how to make the pulses increment the variable?
Any guidance very welcome.
Bryan

clockman

Posts: 16
Joined: Fri Nov 23, 2018 11:28 am

Re: programing

You can do it that way if the timing is right. The key is to check the pin over and over very quickly in the loop() part of your code so you do not miss the 25 ms pulse. You do this with a digital read statement if(digitalRead(2) == HIGH) then increment variable. The thing you have to avoid is recording 2 or more high readings before the pulse goes low. We use similar code called 'state' code when we only want to record a button press once. While the code is not difficult, it is a little hard to follow for beginners. Button presses are not a set duration like your input pulses are so you could just put a 50 ms delay after you get a high reading to give the high pulse time to go low. To increment a variable by 1 you just put ++ after the variable name variableName++;. Then you use another if statement to see if the variable value is 3 if(variableName == 3) do the code to send a pulse. We use the == to compare 2 values and we use = to assign a value, as in variableName = 0;

I can post the code if you want but you seem to want to try it yourself. If you run into problems post the code you have and we can help get it right.

Keith

kcl1s

Posts: 1247
Joined: Tue Aug 30, 2016 12:06 pm

Re: programing

That looks to be just the guidance I needed. I will have ago along those lines and let you know how I get on. I can certainly follow the logic. Thank you again.

clockman

Posts: 16
Joined: Fri Nov 23, 2018 11:28 am

Re: programing

After many trials, much Googling and wearing the verifier out, I have put together some code which seems to have compiled ok. Whether it will work on Trinket remains to be seen! If it will I feed some pulses in and see if it likes them. Not sure how to include code in reply - might have to do with copy and paste, but would welcome any comments.
Code: Select all | TOGGLE FULL SIZE
`const int pulsePin = 2;const int outputPin = 3;int pulseCounter = 0;int pulseState = 0;void setup() {  // put your setup code here, to run once:pinMode(pulsePin,INPUT);pinMode(outputPin,OUTPUT);}void loop() {  // put your main code here, to run repeatedly:pulseState=digitalRead(pulsePin);delay(50); pulseCounter++;if (pulseCounter==3); digitalWrite(outputPin, HIGH);delay(100);digitalWrite(outputPin, LOW);  //hopefully to produce a 1/10 second square wave output;`

Thanks
Bryan

clockman

Posts: 16
Joined: Fri Nov 23, 2018 11:28 am

Re: programing

Bryan,
It is a good start but you are missing a thing or two.

I don't see if you have the original Trinket or the Trinket M0. Pins 3 and 4 on the original Trinket are shared with USB so avoid them if you only need 2 pins. Pins 0 for input and pin 1 for output would be a good choice. Pin 1 also has the on-board LED so you will get a blink when it sends the out pulse.

You are reading the input state with pulseState=digitalRead(pulsePin); but then you only want to do something if the value is HIGH. An if conditional loop is what you use. Here is a reference of how to do it. https://www.arduino.cc/reference/en/language/structure/control-structure/if/. Pay attention to the syntax. Those curly bracket are needed and what is between them is what gets executed when the if statement is true.

You will need to get the syntax right on the second if that checks the pulse count. After you send the out pulse don't forget to set the counter back to 0.

Keep on learning.
Keith

kcl1s

Posts: 1247
Joined: Tue Aug 30, 2016 12:06 pm

Re: programing

Hello again Keith
I hope I am learning but as you pointed out 'if' command syntax can be tricky. Not sure if my useage is ok. Dont think I need to use 'pulseState' - time will tell!. This compiles but I realise that doesnt mean the sketch will produce the desired result.
As you will realise, it is lack of understanding the verify error codes that makes the exercise difficult for novices.
Have I maintained the logic with this latest version, and do you think it is worth putting some input into it and seeing if anything comes out??
Code: Select all | TOGGLE FULL SIZE
`const  int pulsePin = 0;const  int outputPin = 1;int pulseCounter = 0;int pulseState = 0;void setup() {  // put your setup code here, to run once  pinMode (pulsePin,INPUT);  pinMode (outputPin, OUTPUT);}void loop() {  // put your main code here, to run repeatedlydigitalRead(pulsePin);if (pulsePin == HIGH);(pulseCounter ++);(pulsePin==0);delay(50);if (pulseCounter==3);digitalWrite(outputPin,HIGH);delay(100);digitalWrite(outputPin,LOW); // 1/10 sec square pulses?}  `