// Modulus Operator
sbot
Posts: 19
Hello to All,
I am trying to use the Modulus operator to seperate whole minutes and the remaining seconds from a raw total of seconds.
Like this:
seconds//60
What hapens to the seconds if I start with a number less than 60?
I am afraid I will get 0 minutes 0 seconds
I am trying to use the Modulus operator to seperate whole minutes and the remaining seconds from a raw total of seconds.
Like this:
seconds//60
What hapens to the seconds if I start with a number less than 60?
I am afraid I will get 0 minutes 0 seconds
Comments
-Tor
Paul
EDIT:
Here's an example:
Yea, I should have attached the code I had already written, and you could have seen I already had a line separating minutes.
the example you attached was almost exactly what I had. I don't have a stamp here (I'm @ work; in the middle of an oil field; isolated)
so I couldn't test to see if it did what I wanted.
I am a Mud-Logger on a Gas Drilling Rig trying to make my job more accurate and easier using a Stamp. You would not believe the archaic methods used to gather the data I need to do my job.
Thanks,
Oscar
The next to the last line is superceded by the last line, and the last line completely discards the minutes.
thsMins=rsec*100/60 Avoids an answer I can not use like 1.66, and the line above that drops any seconds below 6; because (5,4,3...*100/60) returns answers I cannot use like 5*100/60*=8.333.....The way I have debug set up it would display as .8 minutes, instead of.08minutes. But that is ok I don't need anything smaller than .1 minutes.
You are much more experienced at this than I am, but why would the last line completely discard minutes. Minutes, I thought would be calculated by the line 3 lines above (I need to start numbering my lines)
minutes = SECONDS/60 I mean they are two separate variables with different names . I am sure you are correct, but you are going to have to splain it to me.
Opps wait a minute: never mind
I think you can replace the two red lines by the single statement,
thsMins = rsec/6
That integer division is automatically zero for rsec values less than 6, so your tenths of a minute will be minutes.0 for rsec={0..5} up to minutes.9 for rsec={54..59}.
IF rsec<6 THEN thsMins = rsec*0 Im sure there is a more elegant way to get rid of the remaining seconds I couldnt use, but multiplying by zero seemed the easiest to me.
Thanks Tracy,
Oscar
I will probably be back with questions about hardware then. Although it should be pretty simple. I'll splice into the line that energizes the electromagnet that makes the pen place a tick mark on the chromatograph paper with a resistor to limit the current. That should give me a high on the input pin starting the count. Thank you all for the help. I'll be back
Oscar
I love this stuff. Really makes you think. Wish I knew more.
Thanks,
Oscar
That integer division is automatically zero for rsec values less than 6, so your tenths of a minute will be minutes.0 for rsec={0..5} up to minutes.9 for rsec={54..59}.[/QUOTE]
Tracy,
Wait a minute I just thought of something. If the Modulus returns an answer of zero for anything smaller than the number you are using to mod. The line: rsec = SECONDS//60 will return a zero for any number of seconds smaller than sixty, and that is what I need the most. Fractions of a minute, tenths of minutes.
I’ll have to and a couple of lines to deal with totals less than sixty
Maybe after the line:
SECONDS = SECONDS/2
Add this line…...................IF SECONDS<60 THEN
and this one…...................SECONDS = SECONDS+60
minutes = SECONDS/60 '.....................
rsec = SECONDS//60'...................
thsMins = rsec*100/60
DEBUG "ROP =",DEC minutes,".",DEC thsMins,32,"minutes"
GOTO RESET
Will the rest of the program work if I add the two lines above or do I need ELSE and ENDIF statements
I love this stuff. Really makes you think. Wish I knew more.
Thanks,
Oscar
Have you tested the code? I don't know the stamp, but as I said in my earlier reply a modulus implementation is supposed to handle values lower than the modulus operator just fine. So 51//60 is supposed to return 51, not zero.
-Tor
I don't have anything with me to test. However I'll be going home soon and will gather everything for the next job. I hope you are correct.
Thanks,
Oscar
Thanks for the offer, but I'll be going home in a few days and I have everything I need there.At this location I'm only about 20 miles from civilization, but some sites like in west Texas are really remote.
Thanks again,
Oscar
There's no shortage of good help around here.
You could buy a 24V lamp and couple that to a photocell and use RCTIME.
You could use a diode to half-wave rectify the 22vac and turn on the IRED (w/ appropriate resistor) in an optocoupler and poll/test that on an INPUT pin.
Thanks PJ
In your last program post you still had,
thsMins = rsec*100/60
That gives 1/100ths of a second. All you need for tenths is,
thsMins = rsec/6.
minutes= seconds/60
sec2 = minutes*60-seconds
print "time is: " + minutes + "Min " + sec2 + "Sec"
I don't think so... if seconds is less than 60 then minutes will = 0.
So, 0* 60 = 0
and 0 - seconds = 'negative seconds'
sec2 = seconds - minutes*60
because seconds is the larger number.
That is a valid way to do it, but
seconds // 60
works fine.
By the way, there is a difference between the Stamp and the Propeller in the way they treat the // operator when it has to do with negative numbers. The Prop in Spin recognizes numbers as twos complement, and for example,
-43 // 60 = -43
or
(-43 - N*60) // 60 = -43 ' for any non-negative integer N.
So the negative numbers mirror the positive numbers with respect to the // operator.
The Stamp treats numbers always as positive in that context,
-43 can be written for the Stamp, and printed out as -43 for things like debug, but the math operators treat it as (65536 - 43) = 65493. Under the // operator that works out to,
-43 // 60 = 33 ' not right!.
That is not an issue with time values because in this world time is usually not negative. (Be careful though -- around here -- you never know.
You just have to get around to checking it periodically.
If you need to, you can hang a little cap on the end of that half-wave recitifer, like a smoothing cap in a power supply, to provide a little bit of hang-time from the "pen tick".
Does INPUT pen_volts make Pin 0 an input
I know this is elementry to you and others with thousands of posts, but I'm not sure what these 2 lines do. The rest of the code I understand except it seems backwards to me. I'm using a postive pulse to activate the start wouldn't 1=active be more logical. I'm probably in error in my thinking but seems like you want to make a 1=0=1
What is wrong with my original program ? I Define a pin as an input low, then loop and check that pin each loop for a high. I'm not saying I disagree with what you are saying, I'm just not sure what you are saying.
Thanks again,
Oscar
pen_volts PIN 0 makes an "alias" for Pin0 - so it now has a name
INPUT pen_volts Yes, it makes Pin0 an INPUT
There's nothing wrong with your original. I was just Demo'ing using my general assumption.
Lots of people get stuck in this on=active=1 abstraction. An active state can be a HIGH or a LOW, depending on the circuit.
As you move along you may find it more convenient to test for an "active Low", but don't let that impede your progress.
Comment your code (for your own reference if not others') so that you can remember what your idea was at the time.
It's true! (: