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.

pass the TIME away

Status
Not open for further replies.
20201210_124018.jpg
 
personally, I'd set it at the beginng so you're not at the mercy of someone using it before calling Stage()...
Code:
Sub Stage()
buttons=0      // init buttons BEFORE entering loop
Repeat
    // first loop goes here...
Until (Buttons = 3) //3
                      
      
buttons=0      // init buttons BEFORE entering loop
Repeat
    // second loop goes here...
Until (Buttons = 3) //3

end sub
 
I have the code set up as in post 63 and int buttons at the beginning as in post 63.
The leds indicate the code is progressing but the code reverts back to the first loop? as in post #60
Putting the leds to track code progression was one of Jon's ideas wayyy back when. It helps
 
See in the big red circle how you reset buttons =:0 after the first repeat/until loop?

Follow the big red arrow. Notice how you don't reset it after the second repeat/until loop?

So the next time you get to that subroutine, what happens? Whether or not the buttons are pressed, buttons = 3 and the red LED turns on.

Tumbleweed is also reseting buttons =:0 for each repeat/until loop, just in a better way. Either will work. Time to THINK about how the code reacts.
 
The leds indicate the code is progressing but the code reverts back to the first loop? as in post #60
That's YOUR assumption as to what's going on.

I seriously doubt that. That's why I asked for the ENTIRE code in its current state.
I'm not going to try too hard guessing... for example what's the difference between 'blue1' and 'blue_1'???

Also, now that you have the Stage() subroutine, a better way would be to get rid of the global
'buttons' variable entirely and use a local variable inside the sub.
You'd still be relying on the state of a few other global variables/leds, but that's your problem.

Code:
Sub Stage()

    // 'buttons' holds keypressed info... 
    // bit 0=Go_1 pressed and bit 1=Go_2 pressed
    dim buttons as byte
    
    buttons=0                // clear keypressed info
    Repeat
        If (blue1 = 0) Then
            If (Go_1 = 0) And (blue1 = 0) Then        // player2 button press...
                blue1=1
                blue2=0
                buttons.bits(0) = 1
            EndIf
            If (Go_2 = 0) And (blue_1 = 0) Then       // player2 button press...
                blue_1=1
                blue_2=0
                buttons.bits(1) = 1
            EndIf
        EndIf
    Until (buttons = 3)     // both keys have been pressed
    red=1
    delayms(500)
    red=0
    delayms(500)

    buttons=0                // clear keypressed info
    Repeat
        If (Go_1 = 0) And (blue2 = 0) Then        // player1 button press...
            blue1=1
            blue2=1
            buttons.bits(0) = 1
        EndIf
        If (Go_2 = 0) And (blue_2 = 0) Then       // player2 button press...
            blue_1=1
            blue_2=1
            buttons.bits(1) = 1
        EndIf
    Until (buttons = 3)     // both keys have been pressed
    green=1
    delayms(500)
    green=0
    delayms(500)
End Sub
 
here is my entire code. Am rethinking about the code as is. It appears to work but?
Code:
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2020 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 11/23/2020                                                     *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2020 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 11/11/2020                                                     *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}
Device = 18F43k22
Clock = 8
Config FOSC = INTIO67   'internal oscillator 
//Config MCLRE = off
// some LCD options...


 
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3
#option DIGITALIO_INIT = true
Include "setdigitalio.bas"

Include "utils.bas"
Include "LCD.bas"
Include "ADC.bas"
Include "convert.bas"
Include "intosc.bas"
Include "isrtimer.bas"
Include "utils.bas"
Include "convert.bas"
// read the AD port and scale for 0 - 5 volts...
Function Player_1_Volt() As Word
  result = (ADC.Read(0))  //porta.0
End Function
Function Player_2_volt() As Word
   result = (ADC.Read(1))   //porta.1
End Function

// sampled AD value...

Dim led As PORTC.7         //test yellow
Dim gear1_led As PORTD.0    // player1  shift led
Dim gear2_led As PORTD.1    //   player1  shift led
Dim Gear1_Press As PORTC.2   //player1 shift button
Dim Gear2_press As PORTC.3    //player2  shift button

    //white player2
Dim SHIFT1 As Byte     
Dim yellow1 As PORTB.1       //yellow1
Dim yellow2 As PORTB.2       //yellow3
Dim yellow3 As PORTB.3
Dim green As PORTB.4       // yellow
Dim red As PORTB.5
Dim blue2 As PORTB.0        //player1 stage1
Dim blue1 As PORTB.7         //player1 stage2
Dim blue_1 As PORTC.6
Dim blue_2 As PORTC.5
Dim counter As Byte
Dim ms As Word
Dim RT1 As Word
Dim RT2 As Word
Dim Go_1 As PORTA.7
Dim Go_2 As PORTA.6
Dim Buttons As Byte
Dim stage1 As Byte         //blue leds
Dim stage2 As Byte
Dim index1 As Byte
Dim index2 As Byte
Dim counter1 As Byte
Dim counter2 As Byte
Dim x As LongWord
Dim gear1 As Word
Dim gear2 As Word
Dim Stage_press As LongWord
Sub OnTimer()
    Inc(ms)
End Sub
Sub test1()
    blue1=1
    blue_1=1
    DelayMS(2000)
    Toggle(blue1)
    Toggle(blue_1)
    DelayMS(2000)
End Sub
Sub TEST()
    gear1_led=1
    gear2_led=1
    Buttons = 0
    Repeat  //shift1
        If (Gear1_Press = 0) And (gear1_led= 1) Then        // player1 button press...
            DelayMS(300)
            Buttons = Buttons Or 1
            gear1_led=0
            DelayMS(300)
        EndIf
        If (Gear2_press= 0) And (gear2_led = 1) Then        // player2 button press...
            DelayMS(300)
            Buttons = Buttons Or 2
            gear2_led=0
            DelayMS(300)
        EndIf
    Until (Buttons = 3) //3
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx   
            gear1_led=1
            gear2_led=1
            Buttons = 0
            Repeat   //shift2
                If (Gear1_Press = 0) And (gear1_led= 1) Then        // player1 button press...
                    DelayMS(300)
                    Buttons = Buttons Or 1
                    gear1_led=0
                    DelayMS(300)
                EndIf
                If (Gear2_press= 0) And (gear2_led = 1) Then        // player2 button press...
                    DelayMS(300)
                    Buttons = Buttons Or 2
                    gear2_led=0
                     DelayMS(300)
                EndIf
            Until (Buttons = 3) //3
  //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx                 
                    gear1_led=1
                    gear2_led=1
                    Buttons = 0
                    Repeat     //shift3
                        If (Gear1_Press = 0) And (gear1_led= 1) Then        // player1 button press...
                            DelayMS(300)
                            Buttons = Buttons Or 1
                            gear1_led=0
                             DelayMS(300)
                        EndIf
                        If (Gear2_press= 0) And (gear2_led = 1) Then        // player2 button press...
                            DelayMS(300)
                            Buttons = Buttons Or 2
                            gear2_led=0
                             DelayMS(300)
                        EndIf
                    Until (Buttons = 3) //3
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                            gear1_led=1
                            gear2_led=1
                            Buttons = 0
                            Repeat     //shift4
                                If (Gear1_Press = 0) And (gear1_led= 1) Then        // player1 button press...
                                    DelayMS(300)
                                    Buttons = Buttons Or 1
                                    gear1_led=0
                                     DelayMS(300)
                                EndIf
                                If (Gear2_press= 0) And (gear2_led = 1) Then        // player2 button press...
                                    DelayMS(300)
                                    Buttons = Buttons Or 2
                                    gear2_led=0
                                     DelayMS(300)
                                EndIf
                            Until (Buttons = 3) //3                                   
End Sub

// display results
Sub Display()
    Dim s As String
    Cls
    s=DecToStr(RT1/1000)+"."+DecToStr((RT1 Mod 1000))
    WriteAt(2,1,"timer1       win")            //display players score
    WriteAt(1,1,"rt1   ",s,"     ")
    s=DecToStr(RT2/1000)+"."+DecToStr((RT2 Mod 1000))
    WriteAt(3,1,"timer2     ")            //display players score
    WriteAt(4,1,"rt2   ",s,"     ")
    DelayMS(5000)
    Cls
     s=DecToStr(gear1/1000)+"."+DecToStr((gear1 Mod 1000))
    WriteAt(1,1,"timer1    ")            //display players score
    WriteAt(2,1,"GEAR1   ",s,"     ")
    s=DecToStr(gear2/1000)+"."+DecToStr((gear2 Mod 1000))
    WriteAt(3,1,"timer2     ")            //display players score
    WriteAt(4,1,"GEAR2   ",s,"     ")
    DelayMS(10000)
    Cls
    ' WriteAt(1,1,"counter1 = " ,DecToStr(counter1))
     'WriteAt(3,1,"counter2 = " ,DecToStr(counter2))
    
End Sub

Sub Foul()
    If green = 0 Then
        red=1
        WriteAt(1,1,"FOUL" )
        DelayMS(3000)
        blue1=0 //led is off
        blue2=0
        yellow1=0
        yellow2=0
        yellow3=0
        green=0
        red=0
        Cls
    EndIf
End Sub

Sub End_of_game()
    blue1=0 //led is off
    blue2=0
    blue_1=0
    blue_2=0
    yellow1=0
    yellow2=0
    yellow3=0
    green=0
    red=0
    
    DelayMS(2000)
End Sub


Sub Stage()
//#1 
Repeat
'buttons=0
        If (Go_1 = 0) And (blue1 = 0) Then        // player1 button press...
            blue1=1
            blue2=0
            Buttons = Buttons Or 1
        EndIf
        If (Go_2 = 0) And (blue_1= 0) Then        // player2 button press...
            blue_1=1
            blue_2=0
            Buttons = Buttons Or 2
        EndIf
    Until (Buttons = 3) //3
       red=1
       DelayMS(500)
       red=0
       DelayMS(500)
                      
      
Buttons=0
Repeat

        If (Go_1 = 0) And (blue2 = 0)Then        // player1 button press...
            blue1=1
            blue2=1
            Buttons = Buttons Or 1
            counter1=1
        EndIf
        If (Go_2 = 0) And (blue_2= 0) Then        // player2 button press...
            blue_1=1
            blue_2=1
            Buttons = Buttons Or 2
            counter2=1
        EndIf
       'writeat(1,1,"buttons",dectostr(buttons))
      ' delayms(4000) 
    Until (Buttons = 3) //3
          green=1
          DelayMS(500)
          green=0
          DelayMS(500)
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Timer.Start
   Repeat
   'stage_press=stage_press+1
   DelayMS(500)     
    If (Go_1 = 0) And counter1=1Then        // player1 button press...
            blue1=0
            blue2=1
            'Buttons = Buttons Or 1
        EndIf
        If(Go_2 = 0) And counter2=1Then        // player2 button press...
            blue_1=0
            blue_2=1
            'Buttons = Buttons Or 2
        EndIf
       ' endif
   Until ms=7000
      yellow1=1
          DelayMS(500)
          yellow1=0
          DelayMS(500)
                      
End Sub

Sub Start_race()
     Repeat
        If (Go_1 = 0) And (RT1 = 0) Then        // player1 button press...
            RT1 = ms
            Buttons = Buttons Or 1
        EndIf
        If (Go_2 = 0) And (RT2 = 0) Then        // player2 button press...
            RT2 = ms
            Buttons = Buttons Or 2
        EndIf
    Until (Buttons = 3) //3
End Sub

Sub Gear_shifts()      //lowest rt is first shift light to come on
For x = 0 To 3        //4 shifts
      
      gear1_led=1
      gear2_led=1
      DelayMS(400)
      Repeat       //gear1
        If gear1_led=1 And(Gear1_Press = 0) Then        // player1 button press...
            gear1_led=0
            Buttons = Buttons Or 1
           ' delayms(500)        //a FLAG to keep track
            
            
           ' delayms(500)
            'gear1_led=0
            'gear2_led=0
            'delayms(500)
        'EndIf
        'gear2_led=1
        If gear2_led=1 And(Gear2_press = 0) Then        // player2 button press...
            gear2_led=0
            Buttons = Buttons Or 2
            DelayMS(500)
            
            'delayms(500)
            'gear2_led=0
            'delayms(500)
            
        EndIf
         EndIf
    Until (Buttons = 3) //3
            Buttons=0
    Next
        'GEAR1_LED=0     //turn off shift leds
        'gear2_led=0
        gear1=ms+RT1
        gear2=ms+RT2
      
End Sub



'SetAllDigital
Buttons=0
x=0
Output(gear1_led)
Output(gear2_led)
Output(blue1)
Output(blue2)
Output(yellow1)
Output(yellow2)
Output(yellow3)
Output(green)
Output(red)
Output(blue_1)
Output(blue_2)
Output(led)
Low(gear1_led)      // make led outputs, set 0
Low(gear2_led)
Low(blue1)
Low(blue2)
Low(yellow1)
Low(yellow2)
Low(yellow3)
Low(green)
Low(red)
index1=0         //keep track of staging lights
index2=0
counter1=0
counter2=0
counter=0
stage1=0
stage2=0
Stage_press=0
Input(Go_1)     //button press A.7
Input(Go_2)     //button press A.6
gear1=0
gear2=0
Input(Gear2_press)
Input(Gear1_Press)
//initialize timer
Timer.Initialize(1)
Timer.Items(0).interval=1  //ms
Timer.Items(0).OnTimer=@OnTimer  //timer event handler
Timer.Items(0).Enabled=true

// add your while loop and countdown code here...
While True
led=1
DelayMS(100)

Stage()
'test1()
Wend
    {
    DelayMS(1000)
    yellow1=1
   If (Go_1 = 0) Or (Go_2 = 0) GoTo skipcode         //skipcode is a foul start

    DelayMS(500)
    yellow2=1
    If (Go_1 = 0) Or (Go_2 = 0) GoTo skipcode         //skipcode is a foul start

    DelayMS(500)
    yellow3=1
    If (Go_1 = 0) Or (Go_2 = 0) GoTo skipcode         //skipcode is a foul start

    DelayMS(500)
    If (Go_1 = 0) Or (Go_2 = 0) GoTo skipcode         //skipcode is a foul start
  
    DelayMS(200)
  
    // start race
    ms = 0
    RT1 = 0
    RT2 = 0
    green=1
    SHIFT1=0
    Buttons = 0
    
    Timer.Start()     //start race
    Start_race()
//both players are deep staged
    'for x = 0 to 3
    'DelayMS(500)//1.5 SECONDS BEFORE FIRST SHIFT
    'gear1_led=1  //TURN LED ON
    'gear2_led=1   //led on
    'if gear1_press=0 then
    'toggle(gear1_led)
    
    'if gear2_press=0 then
    'toggle(gear2_led)
    
    'endif
    'endif
    'next
    'DelayMS(500)
    Timer.Stop

'Timer.Adjust(-1500)
Timer.Start
' gear_shifts()
TEST()
  
    
   Timer.Stop()
     Display()      //display timer1 and timer2
skipcode:
      'WriteAt(1,1,"gear11 = " ,DecToStr(gear1))
    ' WriteAt(3,1,"gear2 = " ,DecToStr(gear2))
    ' DelayMS(3000)
     Foul()
     End_of_game()
Wend
 
Did you even attempt to understand this?


Or the clarification here? I typed it really s l o w l y so you could understand.

See in the big red circle how you reset buttons =:0 after the first repeat/until loop?

Follow the big red arrow. Notice how you don't reset it after the second repeat/until loop?

So the next time you get to that subroutine, what happens? Whether or not the buttons are pressed, buttons = 3 and the red LED turns on.

Tumbleweed is also reseting buttons =:0 for each repeat/until loop, just in a better way. Either will work. Time to THINK about how the code reacts.


But in the code you just posted, the one line you needed to add STILL isn't there.
 
here is my entire code
I never should have asked.

It appears to work but?
If you're happy with that, more power to you!
99% of it is completely unnecessary (and I'm talking about the few parts that are actually used)

But in the code you just posted, the one line you needed to add STILL isn't there.
**SIGH**
You mean the one that makes it work? The one that SETS BUTTONS=0 BEFORE CALLING STAGE() A SECOND TIME?
 
**SIGH**
You mean the one that makes it work? The one that SETS BUTTONS=0 BEFORE CALLING STAGE() A SECOND TIME?

Yeah, that would be the one....THE ONE SPECIFICALLY ASKED ABOUT IN THE LATEST VOLLEY. I know nothing about the rest of the code. It's hard enough to follow other people's code when there is SOME LOGIC to it. When it's a incomprehensible nightmare that follows no rhythm or reason, when any advice given given will be ignored anyway, it's just not worth the effort AGAIN.
 
Yeah, that would be the one....THE ONE SPECIFICALLY ASKED ABOUT IN THE LATEST VOLLEY.
Oh, I see now. You're talking about the ONE LINE THAT THIS ENTIRE PAGE OF POSTS HAS BEEN POINTING OUT.

I thought you might have been talking about having to add a const array or ?????
 
True. A constant array us one of MrDEB's favorites. I don't believe he has any clue about why or where to use one, but he likes to fantasize about using one someday.

No, I think we are almost to act 2, where MrDEB sees something that someone posted somewhere else, and decides to toss this version and start over. Actually, we're only on page 4 in the forum, so act 2 may be a ways off yet.
 
only working with this sub route. After making suggested suggestions it works but I need to add in a timed 7 seconds from after the first blue led comes on. Either blue1 or blue_1. got rid of the buttons and using index1 for player1 and index2 for player2. blue1 and blue2 are player#1 blue_1 and blue_2 are player# 2

Code:
Sub Stage()
//#1
index1=0
Repeat

        If (Go_1 = 0) And (blue1 = 0) Then        // player1 button press...
            blue1=1
            blue2=0
            index1 = index1 Or 1
        EndIf
        If (Go_2 = 0) And (blue_1= 0) Then        // player2 button press...
            blue_1=1
            blue_2=0
            index1 = index1 Or 2
        EndIf

    Until (index1 = 3) //3
       red=1                  //  code progression indication
       DelayMS(500)
       red=0
       DelayMS(500)
                      
//stage#2       
index1=0
Repeat

        If (Go_1 = 0) And (blue2 = 0)Then        // player1 button press...
            blue1=1
            blue2=1
            index1 = index1 Or 1
            counter1=1
        EndIf
        If (Go_2 = 0) And (blue_2= 0) Then        // player2 button press...
            blue_1=1
            blue_2=1
            index1 = index1 Or 2
            counter2=1
        EndIf
      
    Until (index1 = 3) //3
          green=1                    //  code progression indication
          DelayMS(500)
          green=0
          DelayMS(500)
          
//DEEP STAGING
'Timer.Start
index1=0      //this section needs to exit AFTER 7 SECONDS
   Repeat
  
       If (Go_1 = 0) And counter1=1Then        // player1 button press...
            blue1=0
            blue2=1
            index1=index1 Or 1
        EndIf
        If(Go_2 = 0) And counter2=1Then        // player2 button press...
            blue_1=0
            blue_2=1
            index1=index1 Or 2
        EndIf
      
   Until ms=700        //need to time for 7 seconds
      yellow1=1
          DelayMS(500)             //  code progression indication
          yellow1=0
          DelayMS(500)
 
Didn't this tread start with explaining that you can't use delays like this? [Hint: yes]

Wasn't it explained how to use a single timer to keep track of times and delays? [Hint: yes]

If you had taken the effort to understand how this worked, do you think you might be able to apply it now?

Do you anybody will be willing to explain the same thing again? [Hint: doubtful]


Think of a delay statements as "stop and wait here." NOTHING is going to happen during a delay, including reading switches.
 
I plan (hopefully) to use one timer. The delay's are temporary. Used to be able to see the LED come on only.
It's 3:17am Saturday. back at it.
Basically got the stage sub route to work but need to put in a timer when the first blue led is on, count for 7000ms then start the "race" keeping track of each players reaction time.
 
Please use correct terminology – then there is the tiniest chance that people may think you know a tiny bit about programming.
 
I know this has been explained, but let's try again....

Every DelayMS() statement stops the program and waits the specified time interval. Any switch pressed during a delay will go undetected. Where you're trying to capture time between button presses, this doesn't work. You simply can not use delay statements where you are trying to detect button presses and the interval between them.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top