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.

Pov triggering code

Status
Not open for further replies.

MrDEB

Well-Known Member
Been awhile since I visited Electro tech online as I have been distracted designing and building a recumbent bike, trike. Have a trike built and making improvements as needed / desired. Well this gets to the addition of a POV led display I want to add. My orginal desine was a BATMAN symbol as the tandem recumbent I am designing anmd building is going to have a skin on frame Batmobile, 4 wheels, two seats with independent gears for both riders. Wanting to have done for the Christmas Parade here locally but time says no way Jose.
So I am going to just add led strings to the spokes and frame of the built trike.
Have started writing code as most pov displays for a bike wheel requires the bike to travel in excess of 5-8mph. Plan to stay below 5mph. Present plan is attach led strings to spokes and light up in some sort of sequence. Utilizing two magnets 180 degrees apart on the bike frame or two hall sensors and one magnet. Time the interval from trigger point A to point B (180 degrees apart) then using the interval as a variable, multiply by 2. The code I have posted is testing to sense the time between button presses (using a Tap-28 board for testing and software uart). This code seems to work but feel I am missing something but what? I found I needed a switch de-bounce as the display screen showed weird outputs.
Using the variable extracted from the time interval to cycle through the data bits. Any suggestions appreciated as this may not be the correct method to go??
Code:
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                  *
*  Author  : [select VIEW...EDITOR OPTIONS]                                *
*  Notice  : Copyright (c) 2013 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 10/08/2013                                                      *
*  Version : 1.0                                                            *
*  Notes  :  testing of trigget timing                                                              *
*          :                                                                *
*****************************************************************************
}
Device = 18F2420
Clock = 8
Include "convert.bas"
Include"suart.bas"
Include"InternalOscillator.bas"
Dim R_un As Word

Dim led1 As PORTA.5
Dim led2 As PORTB.3
Dim count As Word
Dim s1 As PORTB.5
Dim s2 As PORTB.4
dim Trigger as word
UART.SetBaudrate(sbr9600)
UART.SetMode(umTrue)
UART.SetTX(PORTB.7) ' matches the PICkit 2 UART tool input pin when connected to the ICSP connector
Output (led1)
Output(led2)
led1=1
led2 = 1
input (S1)
input (S2)
R_un = 0

While 1 = 1
    if S1 = 1 and R_un <1 then      ' waiting for button press
    delayms(100)  'switch debounce
    R_un = 0
    end if
                                   
    If s1 = 0 and S2 = 1 Then        ' button press
    delayms(100)  'switch debounce
      R_un = R_un + 1  'count time between pressing  of S1 and S2
      Low (led1)        ' led1 is on
      UART.Write("run =",DecToStr(R_un), 13, 10)
      end if
     
      if S1 = 1 and S2 = 1  then  ' time between pressing S1 and S2  no button press
      delayms(100)  'switch debounce
      R_un = R_un +1                ' continue counting tiome between press of S1 and S2
      end if
     
      if S2 = 0 then Trigger = R_un  ' S2 is pressed
      delayms(100)  'switch debounce
      UART.Write("Trigger =",DecToStr(Trigger), 13, 10)
      Toggle (led1)              ' turn off led1
      low (led2)                  ' turn on led2
      delayms(500)
      toggle (led2)              'led2 off
      R_un = 0                    ' reset timer R_un
     
   
      End If
   
     
   
 

UART.Write("run =",DecToStr(R_un), 13, 10)



Wend
 
I can't follow the logic of your code, but here are a couple things that will help you out:

1. LED1 is connected to PortA.5; PortA defaults to analog so you must configure the port to be digital. Include "utile.bas" and use the command SetAllDigital.

2. Switches on the TAP-28 are LOW when pressed. Not entirely sure what you're trying to accomplish in the first if/then statement, but you don't need to "debounce" an unpressed switch.

3. I'm not really clear on what reading two pulses per rev does for you if you're just going to multiply the time by two anyway.
 
After my post I realized what I was doing wrong so started rewriting and getting some progress.
On the switch de bounce, before in inserted delayms(100) after the button press lines I quit getting double readouts on the computer screen using the SUART. I assumed it was button bounce.
If I use one sensor the trike wheel will be going to slow so the leds may be jittery but ? not sure. Not even sure if I need to multiply by 2. I might just use the time interval between point A to point B as one time reference then point B to point A as a second time reference.
This is kinda what I have planed or hopefully accomplish. Right now just trying to output the time betweem button presses. I made the mistake of using S1 as portB.4 and S2 as portB.5 which is backwards.
 
Here is my latest attempt and it appears to work. Tried taking out all the DELAYMS(100) and then the UART screen fails to show anytrhing. WHY, maybe the delays slowed it enough to read?? is only thing I can think of. Reserted and display shows up. I realize this may be going in the wrong direction but it's the only thing I can think of that might work. Using the timer interval between sensor ON / OFF cycling then using that time to display the data.
Code:
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                  *
*  Author  : [select VIEW...EDITOR OPTIONS]                                *
*  Notice  : Copyright (c) 2013 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 10/08/2013                                                      *
*  Version : 1.0                                                            *
*  Notes  :  testing of trigget timing                                                              *
*          :                                                                *
*****************************************************************************
}
Device = 18F2420
Clock = 8
Include "convert.bas"
Include"suart.bas"
Include"InternalOscillator.bas"
Dim R_un As Word

Dim led1 As PORTA.5
Dim led2 As PORTB.3
Dim count As Word
Dim s1 As PORTB.4
Dim s2 As PORTB.5
Dim Trigger As Word
Dim mode As Byte            ' sequence of events
Dim button_1 As Byte          ' S1
Dim button_2 As Byte          ' S2
Dim sensor_delay As Byte      ' debounce

Sub check_buttons()
   
    If mode = 0 And s1 = 1 And s2 = 1 Then mode = 1      ' waiting for button press
        DelayMS(100)  'switch debounce
      R_un = 0 
    End If
   
'xxxxxxxxxxxx
    '  START COUNTING TIME BETWEEN BUTTON PRESSES                               
    If mode = 1 And s1 = 0 And s2 = 1 Then mode = 2      ' S1 button press start count
 
    DelayMS(100)  'switch debounce
   
    End If
   
'xxxxxxxxxxxxxxx
     
'zzzzzzzzzzzz     
    ' STOP COUNTING INTERVAL
    Trigger = R_un
      If mode = 2 And s1 = 1 And s2 = 0 Then mode = 3 ' time between pressing S1 and S2  no button press
      DelayMS(100)  'switch debounce
      End If
     
      If mode = 3 And s1 = 1 And s2 = 1 Then mode = 0
      End If
  End Sub
'zzzzzzzzzzzzzzzz 
Sub increment_timer()

    If mode = 0 Then R_un = 0
    End If
    If mode = 2 Then Inc (R_un)
    End If
End Sub

Sub display()
    UART.Write("timer =",DecToStr(R_un), 13, 10)
    delayms(500)
    UART.Write("mode =",DecToStr(mode), 13, 10)
End Sub

UART.SetBaudrate(sbr9600)
UART.SetMode(umTrue)
UART.SetTX(PORTB.7) ' matches the PICkit 2 UART tool input pin when connected to the ICSP connector

Output (led1)
Output(led2)
led1=1
led2 = 1
Input (s1)
Input (s2)
R_un = 0
mode = 0
While 1 = 1
    check_buttons
    increment_timer
    display
Wend
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top