Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

code does not play fair??

Status
Not open for further replies.
Burt,

He's testing the state of a port pin against the variable Pressed.

The port pin can be either 0 or 1; if pressing the switch changes the pin to a low state, and if Pressed = 0, it's a valid test.

I use a constant for Pressed so I don't need to remember if a switch is high or low when reading the code in the future.


Regarding MrDEB's comment that everyone here is a professional programmer...he's wrong about that too. When I got my first JDM programmer and a 28 pin PIC dev board, I started reading Digital-DIY, MrDEB was already posting about trying to use a 24 volt sensor to a 5 volt PIC input. He was trying to use a complex circuit to avoid buying an appropriate sensor for a couple bucks.

MrDEB may not give up, but he requires an inordinent amount of help to do even the simplest things.
 
He's reading port a with 4 switches pressed doesn't need to be a byte. It's a bit test. And as he has it pressed can be set with any that's not a good test. He's going to get error readings
 
I use the technique as I have explained. It makes it easy to understand my code when reading through it.

I use it to check the state of a single defined port pin - whether that defined port pin representing a switch is pressed or not.

Code:
Const Pressed = 0

Dim PortB.0 as S1
Dim PortB.1 as S2

If S1 = Pressed then
    'take some action 
End If

If S2 = Pressed then
    'take some action
End If

While I am loath to defend anything MrDEB does, this technique is effective and, in the case of reading individual push button switches, simplifies code.

In the case under discussion, I would not have followed the approach MrDEB took. If we believe what he's saying, he's using a SP4T switch, requiring 4 port pins to read. We don't know for a fact which switch he used, as the data sheet he linked to includes switches of a number of configurations.

What I would have done is to use either a 4-bit encoded rotary switch or a 10 position BCD (binary codes decimal) switch - I would suspect these are the more common type in this form factor and may indeed be what MrDEB has installed on his board. If this is the case, 4 port pins would provide either 16 or 10 values. In this case, the 4 port pins should be read as a group and decided to 16 (or 10) values. The same amount of hardware would provide 16 options instead of 4.
 
this is a rotary switch so pressing two switches is not possible.
thanks for inquiring without any disparaging remarks but I get a kick out of Jonseas cartoons
 
Since you've missed 5 references to knowing exactly what switch you have used, would you please enlighten us with the exact part number?

You also have shown no interest in the discussion of a SP4T vs binary vs BCD switch, which is fascinating given the great efforts you go to to understand everything discussed.
 
Based on the discussion and the drawing in post #6, it seems we're using this switch and it's wired for active low signals on the A4..A1 inputs. Correct?

MrDeb's Switch.png
 
Last edited:
Please tell me if I have this right, guys? If you sample the active low switches on PORTA bits b4 through b1 you would get one of the following values (ignore unused bits);
Code:
    PORTA
  ---1110-  Position 0
  ---1101-  Position 1
  ---1011-  Position 2
  ---0111-  Position 3
It seems to me that you would want to act only on a switch change which implies using a switch state latch or switch state memory variable that contains the value of the switches from the previous sample. So why not read PORTA into a variable at the beginning of the main program loop and compare it to the old sample variable? If the switch has changed, update the old sample variable and perform the actions needed. Pseudo code example;
Code:
        loop
          delay_ms(25)                 ' 25-msec loop/debounce interval
        '
        ' sample switches and check for a switch position change
        '
          swnew = PORTA & 0b00011110   ' sample switches on b4..b1
          if swnew <> swlat then       ' if switch has changed position
            swlat = swnew              ' update switch state memory
            if SW0 = 0 then            ' if swnew.b1 = 0 (swnew = 00011100)
              do something             '
            endif                      '
            if SW1 = 0 then            ' if swnew.b2 = 0 (swnew = 00011010)
              do something             '
            endif                      '
            if SW2 = 0 then            ' if swnew.b3 = 0 (swnew = 00010110)
              do something             '
            endif                      '
            if SW3 = 0 then            ' if swnew.b4 = 0 (swnew = 00001110)
              do something             '
            endif                      '
          endif                        '
        '
        ' other loop activities
        '
        endloop                        '
Does this analysis help at all?

Cheerful regards, Mike
 
Last edited:
If the switch is as MrDEB's scant description, it may well be that switch Mike. But the data sheet lists BCD and binary versions as well and MrDEB has a rather long history of a loose relationship to the details.

If the switch is SP4T, only one bit can be low at a time, so MrDEB's series of if/then statements would work.

I would use your method of reading the 4 bits at once and acting on the binary number. This aapproach also supports the use of a BCD switch for 10 options or a binary switch for 16. Unless there is some reason why four or few options is the logical choice, I would have used one of the other switch options to provide more flexibility.
 
You have pressed as byte. I don't see the need to be a byte and as it looks to me with Mrdeb code when you turn the switch you could set each if then to error switch bounce.
I see how you was using pressed Mrdeb code can take up to 1 second to read a second switch change it's not good code to read switches.
Code:
IF Pg1=pressed THEN 
Ch_10=1 //led on c.5
DELAYMS(1000)
Ch_10=0
DELAYMS(500)
END IF
That don't look like your code Jon maybe a copy and paste mistake on his part.
Code:
Const Pressed = 0

Dim PortB.0 as S1
Dim PortB.1 as S2

If S1 = Pressed then
    'take some action 
End If

If S2 = Pressed then
    'take some action
End If
 
I'm not defending, anything MrDEB has done here, except the technique of using a constant to define the test condition.
 
Jon I'm not saying that what you do is wrong He was using a byte for pressed so I'm thinking he was going to test the switch port.
Like Mike - K8LH post 27
That way he has a dead set of what the switch is setting at.
 
MrDEB still has not identified the exact switch he has installed on the board.
 
...except the technique of using a constant to define the test condition
Which would have been great except unlike you, he's defined it as a byte variable instead of a 'const'.

My suggestion at this point is to run 'pressed' through RandGen.bas.
Then maybe we'll start to get something that makes sense!
 
Here's a random thought for MrDEB's project before he converts too many LEDs to hazardous waste.

Aluminum circuit boards are available on eBay for mounting LEDs. Single LEDs are typically mounted on these "star" boards. Note that these circuit boards help with heat removal but won't provide enough heatsinking by themselves, particularity with 3 watt LEDs.
tmp_7612-SmartSelectImage_2017-05-27-14-01-511013055549.png


tmp_7612-SmartSelectImage_2017-05-27-14-02-07969048300.png

You might get away with a larger board designed for more LEDs without additional heatsinking.

tmp_7612-SmartSelectImage_2017-05-27-14-03-171126453950.png
 
I mentioned the Aluminium circuit boards and M Smith connected 4 of the 3w leds in series and said that they don't get hot??
My suggestion was to put 5 leds in series so no resistor is needed.The 140 wire wound resistors, at $1.20 each aprox depending on quanity was really not needed as this is only a 15 minute display for 5-7 times come September. Remember this is a non profit set up for a school and he is footing the bill upwards of $1000. All 10 boards are assembled and ready for shipment after I get the .1uf smd resistors.
As for the switch, I posted a link to the 4 position rotary switch. After I inserted SETALLDIGITAL into the code the circuit works just fine.
Thanks for the nudge to solution. https://www.mouser.com/ds/2/96/220-786387.pdf
It is the 4 position single pole switch that I have the all 4 terminals excluding the ground common, tied high via 4-10k resistors.
 
??? I just stated what is going on with present project. Failed to give Jonsea credit for suggesting using "pressed" to get the code working in a similar project.
Adding in that mr Smith is the person who the band led project is for and he is footing the bill. I might add that I am doing this project for free seeing how the cost of a pro would be to prohibitive.
The switch works just fine. and thank you for your concern.
As for understanding, I understand entirely what the the project is doing and voiced my concerns the Mr Smith.
I never stated I was a professional in this line of work. I perhaps go off the deep end with ideas but most inventors do that anyway.
And yes I consider myself an inventor and an artist.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top