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.

Swordfishbasic and swiches

Status
Not open for further replies.

be80be

Well-Known Member
How can I make the led stay on with the sw and stay off with the sw.

Here the code I started
Code:
Device = 18F1220
Clock = 8
Config OSC = INTIO2                // Use the Internal Oscillator

Include "utils.bas"                    
Dim sw As PORTB.3                   // Assign an alias for the switch
Dim LED1 As PORTB.7                  // Assign an alias for "LED"
Dim LED2 As PORTB.6
Dim LED3 As PORTB.5
Dim LED4 As PORTB.4

// Start Of Program...
OSCCON = %01111111                  // Sets up the internal oscillator
SetAllDigital                       // Make all Pins digital I/O's
    Low(LED1)                            // Make the LED pin an output and set it low
    Low(LED2)
    Low(LED3)
    Low(LED4)
      
While sw = 1
 wend
 While true
 Repeat
 Until Sw = 1    
    LED1 = 1                         // Turn on the LED
    LED2 = 1
    LED3 = 1
    LED4 = 1
    DelayMS(500)                    // Delay for half a second
    
    LED1 = 0                         // Turn off the LED
    LED2 = 0
    LED3 = 0
    LED4 = 0                           // Delay for half a second
    DelayMS(500) 
Wend
thanks for any help
 
Last edited:
You would just do,
Code:
while true
    if(Sw=1) then
        LED1=1		//add as many LEDs as you wish
    else
        LED1=0
    endif
wend

Mike.
 
Thanks Mike that works to but not what I'm looking for if the switch is pressed it changes state from low to high so all you get is it toggles the led from low to high. I want the led to stay on the first press and stay off the second press so on and so on. I can do this with two switches why is so hard with 1
 
Last edited:
Try,
Code:
Dim previous as bit
Dim key as bit

key=0
while true
    DelaymS(10)
    previous=key
    key=Sw1
    if(key=1 and previous=0) then
        toggle(LED1)
    endif
wend

Mike.
 
Mike works like charm I almost had it right but I left out the part with Key=Sw1 thanks a lot your the man.
 
This is what I'm trying to do your code works on a port pin only
Code:
Device = 18F1220
Clock = 8
Config OSC = INTIO2                // Use the Internal Oscillator

Include "utils.bas"
Dim sw As Bit
Dim previous As Bit                //saves previous key press
Dim key As Bit                     //saves new press
Dim Sw1 As PORTB.3                 // set port for switch
Dim LED1 As PORTB.7                  // Assign an alias for "LED"
Dim LED2 As PORTB.6
Dim LED3 As PORTB.5
Dim LED4 As PORTB.4

OSCCON = %01111111                 // sets up the internal oscillator
key=0                              // sets key to 0
While true                         // test to see if true
    DelayMS(10)                    //delay for key press
    previous=key                   //moves value to save in previous
    key=Sw1                        // reads new value
    If(key=1 And previous=0) Then  // see if true
    sw  = 1
        Else sw = 0               // turns Led on if true
    EndIf
Wend
 sw = 0
 While true
 Repeat
 Until sw = 1    
    LED1 = 1                         // Turn on the LED
    LED2 = 1
    LED3 = 1
    LED4 = 1
    DelayMS(500)                    // Delay for half a second
    
    LED1 = 0                         // Turn off the LED
    LED2 = 0
    LED3 = 0
    LED4 = 0                           // Delay for half a second
    DelayMS(500) 
Wend
Oh and my code still don't work lol
 
Last edited:
be80be, I'm not basic expert but you have a while true loop where you read the sw but you never leave the loop?!?!

Code:
Device = 18F1220
Clock = 8
Config OSC = INTIO2                // Use the Internal Oscillator

Include "utils.bas"
Dim sw As Bit
Dim previous As Bit                //saves previous key press
Dim key As Bit                     //saves new press
Dim Sw1 As PORTB.3                 // set port for switch
Dim LED1 As PORTB.7                  // Assign an alias for "LED"
Dim LED2 As PORTB.6
Dim LED3 As PORTB.5
Dim LED4 As PORTB.4

OSCCON = %01111111                 // sets up the internal oscillator
key=0                              // sets key to 0
While true                         // test to see if true
    DelayMS(10)                    //delay for key press
    previous=key                   //moves value to save in previous
    key=Sw1                        // reads new value
    If(key=1 And previous=0) Then  // see if true
    sw  = 1
        Else sw = 0               // turns Led on if true
    EndIf
Wend
[COLOR="Red"] 
//program will NEVER get here
sw = 0
 While true
 Repeat
 Until sw = 1    
    LED1 = 1                         // Turn on the LED
    LED2 = 1
    LED3 = 1
    LED4 = 1
    DelayMS(500)                    // Delay for half a second
    
    LED1 = 0                         // Turn off the LED
    LED2 = 0
    LED3 = 0
    LED4 = 0                           // Delay for half a second
    DelayMS(500) 
Wend[/COLOR]
 
Ok this toggles a led on and keeps it on tell i press the 1 switch then it turns it off
Code:
Device = 18F1220
Clock = 8
Config OSC = INTIO2                // Use the Internal Oscillator

Include "utils.bas"
Dim previous As Bit                //saves previous key press
Dim key As Bit                     //saves new press
Dim Sw1 As PORTB.3                 // set port for switch
Dim LED1 As PORTB.7                  // Assign an alias for "LED"

OSCCON = %01111111                 // sets up the internal oscillator
key=0
While true
    DelayMS(10)
    previous=key
    key=Sw1
    If(key=1 And previous=0) Then
        Toggle(LED1)
    EndIf
Wend
you can only toggle a port pin I want to toggle 4 port pin with it on thay all bink with it off there off thats what this part is but
Code:
sw = 0
 While true
 Repeat
 Until sw = 1    
    LED1 = 1                         // Turn on the LED
    LED2 = 1
    LED3 = 1
    LED4 = 1
    DelayMS(500)                    // Delay for half a second
    
    LED1 = 0                         // Turn off the LED
    LED2 = 0
    LED3 = 0
    LED4 = 0                           // Delay for half a second
    DelayMS(500) 
Wend
That code makes absolutely no sense. What are you trying to do?
] You are so right
 
Last edited:
You can put whatever you want in the if statement,
Code:
    If(key=1 And previous=0) Then
        LED1 = 1                         // Turn on the LED
        LED2 = 1
        LED3 = 1
        LED4 = 1
        DelayMS(500)                    // Delay for half a second
        LED1 = 0                         // Turn off the LED
        LED2 = 0
        LED3 = 0
        LED4 = 0                           // Delay for half a second
        DelayMS(500) 
    EndIf

Whilst this will do what you want, it is obvious that you do not understand program flow. Try reading the manual or finding a tutorial on basic so you understand how while..wend and repeat..until work.

Mike.
 
I no my feet get cold when I sit to long HA HA thanks for the help Mike and I'm reading my A off about Control flow arhi There is not much out there on swordfish basic. and what is there just using the modules in it. which is good but you don't see the flow of it and thay don't make a modules for what i want any how don't this look better
Code:
 Device = 18F1220
Clock = 8
Config OSC = INTIO2                // Use the Internal Oscillator

Include "utils.bas"
Dim previous As Bit                //saves previous key press
Dim key As Bit                     //saves new press
Dim Sw1 As PORTB.3                 // set port for switch
Dim LED1 As PORTB.7                  // Assign an alias for "LED"
Dim LED2 As PORTB.6
Dim LED3 As PORTB.5
Dim LED4 As PORTB.4

OSCCON = %01111111                 // sets up the internal oscillator
SetAllDigital                       // Make all Pins digital I/O's
    Low(LED1)                            // Make the LED pin an output and set it low
    Low(LED2)
    Low(LED3)
    Low(LED4)
key=1
While true
    DelayMS(10)
    previous=key
    key=Sw1
    If(key=0 And previous=1) Then
        LED1 = 1                         // Turn on the LED
        LED2 = 1
        LED3 = 1
        LED4 = 1
        DelayMS(500)                    // Delay for half a second
        LED1 = 0                         // Turn off the LED
        LED2 = 0
        LED3 = 0
        LED4 = 0                           // Delay for half a second
        DelayMS(500) 
    EndIf   
Wend
but it just turns on when pressed gos off when let up the key. Thank agin Mike and arhi
 
That should turn on the LEDs for 0.5 seconds whenever the key is pressed. Is that not what you wanted it to do?

Mike.
 
It dose that i change the delay to 1500 and i can see what it's doing
I think i need to read a lot more what i was trying to do is make it Bink when i press the key and keep blinking till I press the key gin.
 
This should do what you require. Work through it and see if you follow what it is doing.

Code:
Dim Count as integer

Count=0
While true
    delayMS(10)
    if (Sw1=1) then             //if switch pressed then
        Count=Count+1           //flash LEDs
        if(Count<50) then       //is it first 0.5S
            LED1 = 1            // Turn on the LED
            LED2 = 1
            LED3 = 1
            LED4 = 1
        Else                    //if second half of second
            LED1 = 0            // Turn off the LED
            LED2 = 0
            LED3 = 0
            LED4 = 0            
        Endif
        if(Count=100) then      //reset after 1 second
            Count=0
        endif
    else                        //else if key not pressed
        Count=0                 //reset count
        LED1 = 0                // Turn off the LED
        LED2 = 0
        LED3 = 0
        LED4 = 0                
    EndIf   
Wend

Mike.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top