want to cycle in a FOR NEXT loop but can't even get one led to come on unless I just go for portb.0 = 1
want to dimension the leds to work with the ports but use leds(x)
pretty basic but ??
I tried portb.0 = leds(0) but it won't work.
It's pretty much impossible to figure out what you're trying to do with that....um....code.... you posted. Perhaps you could explain it in some detail.
Taking a guess, if you have LEDs connected to all of the pins on port B, and you're trying to light each in turn, you could use the rotateshift
command which is indicated by a double greater-than or less-than symbols.
I always have to fiddle with the syntax because I don't use it often.
If you rotates shift 1 bit left, starting with
%00000001
yields
%00000010
then
%00000100
then
%00001000
and so on.
The Swordfish Basic syntax is the same as the C syntax as I recall.
Note: This shift command should not be confused with shifting out data to a serial shift register. That's a totally different usage.
I used the wrong term above. The correct command is the bitwise-operator shift. The bits in a byte are shifted right(>>) or left(<<) the number of places the number after the command specifies.
my guess too is that this line: leds(0) = portb.0 just passes a single value one time only.
not sure exactly how myself, but you need to define it, something like:
#define leds(0) = portb.0
... then the compiler will understand that when you say leds(0), you actually mean portb(0)
For many years now, all I code is previously outlined in a flow diagram. Double advantage: the algorithm becomes clear prior coding and writing the code becomes just a clerical work. It works for me.
This will make LEDs connected to Port B light up sequentially 1 at a time for a tenth of a second. Note that the shifted bit falls off the end, so you have to replace the bit to start over.
LEDs on when the port pin is low? Shift a zero across instead of a 1.
Code:
PortB = 1
Delayms(100)
For I = 1 to 7
PortB = Port B << 1
Delayms(100)
Next
For many years now, all I code is previously outlined in a flow diagram. Double advantage: the algorithm becomes clear prior coding and writing the code becomes just a clerical work. It works for me.
Could we / I get a tutorial on pointers here ! I always have to mess around (C) after compiler complains like.. ' using an int as a pointer without a cast' ... what ever that means, still seems to work, eventually error goes away usually don't know how i fixed it .. I understand the principal , ( perhaps) but where do I put the * ... perhaps a different thread Ian ?
in post 9 I used openoffice but I guess it was not a good choice.
I want to dimension the array of led(x) and swt(x)
basically so I can use
led(0) as portb.0
led(1) as portb.1
using same context
for x = 0 to 7
led(x) = 1
delayms(500)
led(x) = 0
next
this is supposed to blink the led on portb.0 and b.1
am mad at myself that I can't get this to work. I have a 18f4520 pic with edited code for the 18f2221 but it refusses to work. Decided to try a truth table where I have 8 swtiches and 8 leds. when swt(1) goes LOW then led(1) goes high.
upon making swt(1) LOW a second time then led(1) goes LOW
here is a snippet of the code I was hacking on.
"IF swt0 = 1 AND led0= 0
THEN
led0 = 0
END IF
IF swt0 = 0// AND led0 = 0 // button press
THEN
led0 = 1
DELAYMS(500) // switch debounce
END IF
IF swt0 = 0 AND led0 = 1 'second time button press
THEN
DELAYMS(500)
led0 = 0
END IF"
sorry but I tried using insert code but it won' allow it.
this thread is trying to slim down the code, make it easier than what I have in https://www.electro-tech-online.com...wrote-it-works-but.149857/page-3#post-1285641
if I dimension the arrays led() and swt() then put it all in a for next loop but looking at using a truth table and contemplating a CASE SELECT code using the truth table in a loop to turn on and off the correct led/
You've rather unclearly stated what you expect code to do, but not what you want the switches and LEDs to do.
In the second part of your post, I believe you're trying to toggle each LED when its button is pressed. You’re making this way too difficult.
Code:
While 1 = 1
If S1 = Pressed then
toggle (LED1)
DelayMS(500)
End If
If S2 = Pressed then
toggle(LED2)
DelayMS(500)
End If
.....
If S8 = Pressed then
toggle (LED8)
DelayMS(500)
End If
Wend
I think what's needed is a subroutine that will toggle LED(x).
Something like,
Code:
Sub light(led As Byte)
if(led<8) then
porta = porta xor (1<<(led and 7))
elseif(led<16) then
portb = portb xor (1<<(led and 7))
elseif(led<24) then
portc = portc xor (1<<(led and 7))
endif
End Sub