0

programing
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: programing

by kcl1s on Sat Dec 01, 2018 10:59 am

Bryan,
pulsePin is the variable that holds your pin number so you can not use it to hold other values. pulseState can hold the of the pin state. That is why you need to change this digitalRead(pulsePin); to this pulseState = digitalRead(pulsePin); to save the state of the pulsePin in the variable pulseState.



Keith

kcl1s
 
Posts: 1080
Joined: Tue Aug 30, 2016 12:06 pm

Re: programing

by kcl1s on Sat Dec 01, 2018 11:02 am

Bryan,
You have been working hard at this so I will just post the code I came up with. I am using the pin numbers directly so you don't get confused with their variable names.

Code: Select all | TOGGLE FULL SIZE
int numPulses = 0;  // declare a variable to hold number of pulses

void setup() {
  // put your setup code here, to run once:
  pinMode(1, OUTPUT);   //wire pin 1 for output and pin 0 for input
}

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(0) == HIGH){
    numPulses++;        //This adds 1 to the variable numPulses
    if(numPulses > 2){  //If we have 3 pulses send signal.   
      digitalWrite(1, HIGH);
      delay(250);
      digitalWrite(1, LOW);
      numPulses = 0;       //Reset pulse count to 0
    }
    else{               //if less than 3 pulses just wait
      delay(50);        //so we don't read an input twice
    }
  }
}

I don't have a Trinket to try it on but it should be close to what you need.

Keith

kcl1s
 
Posts: 1080
Joined: Tue Aug 30, 2016 12:06 pm

Re: programing

by clockman on Wed Dec 05, 2018 11:52 am

You are spoiling me Keith!!
Sorry for delay in responding.
I have just uploaded to Trinket and all seems to be ok.
Its now down to me to get some pulses into it and see how I (we) get on.
May take a day or so but will certainly let you know. Meanwhile I shall be analysing the sketch to make sure I understand how you have achieved the result, and learning form it.
I have looked at 'change of state' as an alternative possibility but it seems to need serial which I believe the Trinket does not handle.
So thanks again and "watch this space" !
Bryan

clockman
 
Posts: 15
Joined: Fri Nov 23, 2018 11:28 am

Re: programing

by clockman on Thu Dec 06, 2018 10:32 am

Hello Keith
I have tried the code with hand entered 'pulses'. Difficult to get them short enough but there is definitely a flash from the on board led on the third input !! So your code is good for a Trinket (not MO). I also had to put a pulldown resistor on the input pin to stop it doing its own thing.
Problem now is my part of the show. I am having difficulty with the pulse output from my pendulum. I guess it is down to the poor quality of the Chinese drive circuitry. So I have some sorting to do - or buy a new one! For £1.50 its hardly worth plugging the soldering iron in ! Once I get the electronics working I then have to make the metal bits for the clock,
I will keep you advised, but, with the festive season commitments, it might take a while - like next year !
I hope to send you a photo of the finished article eventually.
Thank you again for your help.
All the best to you.
Bryan

clockman
 
Posts: 15
Joined: Fri Nov 23, 2018 11:28 am

Re: programing

by clockman on Thu Dec 13, 2018 12:17 pm

Hello again Keith. Just to keep you updated.
Project is on hold. The simplest of Chinese circuits has decided to stop working. So I have replacements on order from China. Delivery can be up to six weeks so it is going to be next year I think.
However, rest assured that when I get it together again I will let you know how our combined efforts are performing.
Meanwhile can I wish you a merry Christmas and the best for 2019
Bryan

clockman
 
Posts: 15
Joined: Fri Nov 23, 2018 11:28 am

Re: programing

by kcl1s on Thu Dec 13, 2018 6:59 pm

Meanwhile can I wish you a merry Christmas and the best for 2019
Bryan

Thank you, and the same to you and yours.

Keith

kcl1s
 
Posts: 1080
Joined: Tue Aug 30, 2016 12:06 pm

Re: programing

by clockman on Fri Jan 25, 2019 5:39 am

Good morning Keith
Update report. Got the new parts which don't seem to produce the same pulses out in spite of seeming identical. However after a bit of fiddling I have managed to obtain suitable pulses which I have fed into the Trinket. AND IT WORKS BEAUTIFULLY!!
But the Trinket is finding a lot more pulses than I was aware of and I have had to increase the count significantly. but I have finished up with 1 second pulses which was the object of the exercise. So your code is perfect.
However, I overlooked the fact that my slave clock unit needs pulses of alternate + and - polarity! So I now have to find a way round that!
If I manage it I am inspired to carry on with the metalwork as soon as it is warm enough in the outside workshop. So eventually I hope to have a working clock in which case I will send you a photo - but don't hold your breath!!
Thank you very much for your help with this. Coding is clearly something you are experienced in. I doubt if I will be involved again - but you never know.
Best Regards
Bryan

clockman
 
Posts: 15
Joined: Fri Nov 23, 2018 11:28 am

Re: programing

by clockman on Mon Feb 04, 2019 11:23 am

Hello Keith
I have made some progress with my code. I think I told you that my slave clock needed alternate pos and neg pulses. Clearly neg is not available from the Trinket but the clock can be fooled to accept alternate high and low pulses and this can be obtained by passing the positive pulses through a capacitor. So I have finished up with this ---

Code: Select all | TOGGLE FULL SIZE
int numPulses = 0;  // declare a variable to hold number of pulses

void setup() {
  // put your setup code here, to run once:
  pinMode(1, OUTPUT);   //wire pin 1 for output and pin 0 for input
}

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(0) == HIGH){
    numPulses++;        //This adds 1 to the variable numPulses
    if(numPulses > 5){  //If we have 6 pulses send signal.   
      digitalWrite(1, HIGH);
      delay(950);
      digitalWrite(1, LOW);
      delay(50);
     
      numPulses = 0;       //Reset pulse count to 0
    }
    else{               //if less than 6 pulses just wait
      delay(50);        //so we don't read an input twice
    }
  }
}


This seems to work ok.
Then I got a bit ambitious. I use an electronic timer for clocks which works with pulses. So I added to the code to give a separate out put on Pin 2 ----

Code: Select all | TOGGLE FULL SIZE
int numPulses = 0;  // declare a variable to hold number of pulses

void setup() {
  // put your setup code here, to run once:
  pinMode(1, OUTPUT);   //wire pin 1 for output and pin 0 for input
  pinMode(2, OUTPUT);   //Wire pin 2 for output to Microset Timer
}

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(0) == HIGH){
    numPulses++;        //This adds 1 to the variable numPulses
    if(numPulses > 5){  //If we have 6 pulses send signal.   
      digitalWrite(1, HIGH);
      delay(950);
      digitalWrite(1, LOW);
      delay(50);
      digitalWrite(2, HIGH);
      delay(50);
      digitalWrite(2, LOW);
      delay(50);
      numPulses = 0;       //Reset pulse count to 0
    }
    else{               //if less than 6 pulses just wait
      delay(50);        //so we don't read an input twice
    }
  }
}


So all looking good. May have to adjust pulse width on output 2 when tested with timer. Just got to wait now for warmer weather so that I can get into my workshop to make the metalwork., May take a while!! Will be in touch eventually.
Thanks and regards
Bryan

clockman
 
Posts: 15
Joined: Fri Nov 23, 2018 11:28 am

Re: programing

by kcl1s on Mon Feb 04, 2019 2:53 pm

Byran,
Thanks for the update. Glad things are going well.

Keith

kcl1s
 
Posts: 1080
Joined: Tue Aug 30, 2016 12:06 pm

Re: programing

by clockman on Thu Mar 07, 2019 5:51 am

Hello again Keith
After the excitement of seeing the slave clock ticking round in 1 sec steps, it was a shock to find that the clock continued to go when the pendulum was stopped !! That was no use since I need the pendulum to be providing the time and not the code. So that was a setback and after many attempt and adjustments I was unable to change the situation.

So I did more research and found some code to count button presses using State Change.
I made necessary changes to adapt it, and left out some serial code used for printing, and low and behold I have code which produces the required output and doesn't produce output when input is removed.


Code: Select all | TOGGLE FULL SIZE
const int pulsePin = 0;
const int outputPin = 1;

int pulseCounter = 0;
int pulseState = 0;
int lastPulseState = 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);
if (pulseState != lastPulseState){
  if (pulseState == HIGH){
    pulseCounter++;
  } else {
  }   
     lastPulseState = pulseState;
     if (pulseCounter% 6 == 0) {
      digitalWrite(outputPin, HIGH);
      delay(500);
          } else {
      digitalWrite(outputPin, LOW);
 
     }
  }  }




So after a serious set back the project is on again. Hopefully with no more problems.

Have to say though, the { and } caused me a lot of problems, and I don't understand how the % remainder function works -- but it seems to do the trick!

Regards Bryan

clockman
 
Posts: 15
Joined: Fri Nov 23, 2018 11:28 am

Re: programing

by kcl1s on Thu Mar 07, 2019 8:16 am

Bryan,
Glad you found better code.

So in this line if (pulseCounter% 6 == 0) the remainder function makes the if statement true only if pulseCounter is evenly divisible by 6. (6, 12, 18, 24...). The pulseCounter++ keeps adding 1 forever. An int variable range is -32,768 to 32,767. pulseCounter will increase to 32767 then the next ++ will change it to -32768. This is called rollover The remainder % function works for 0 in the negative side ok but all other remainders will be a negative number. You will loose a few pulses on rollover. 32766 has remainder 0 but you will get another remainder 0, four pulses later at -32766.

I try to reset my counters instead of letting them rollover if I can.

Keith

kcl1s
 
Posts: 1080
Joined: Tue Aug 30, 2016 12:06 pm

Re: programing

by XRAD on Thu Mar 07, 2019 5:29 pm

I would use 'unsigned long' for the pulse counter, assuming it will run for a while, those ticks will add up.....you are building an RPM meter. you need a way to count pulses and reset the count and keep it running and counting...

https://www.arduino.cc/reference/en/lan ... ignedlong/

https://www.gammon.com.au/forum/?id=11504

steps to coding:
project design
hardware
read the pulses
count the pulses
do something else?

You can uses something like this to reset the count. There are many ways to do this.
Code: Select all | TOGGLE FULL SIZE
if (val >=3){
val=0;
}


cool project!

XRAD
 
Posts: 221
Joined: Sat Nov 19, 2016 3:28 pm

Please be positive and constructive with your questions and comments.