Led flash speed project?

ChedderCheeser

Senior Member
Ok.. look at this code and help me fix the obvious issue.
Code:
main:
	do
		high LED
		pause TIME
		low LED
		pause TIME
		
		if TIME < 500 Then
			inc TIME	
		Else If TIME > 500 Then
			dec TIME
		End If	
	loop
As you can see, there is a bit of an issue. I want it sto slow down then speed up. Anyway.. thanks! PLEASE REPLY ASAP!
 

ChedderCheeser

Senior Member
Ok.. look at this code and help me fix the obvious issue.
Code:
main:
	do
		high LED
		pause TIME
		low LED
		pause TIME
		
		if TIME < 500 Then
			inc TIME	
		Else If TIME > 500 Then
			dec TIME
		End If	
	loop
As you can see, there is a bit of an issue. I want it sto slow down then speed up. Anyway.. thanks! PLEASE REPLY ASAP!
anyone???

The message you have entered is too short.
 

inglewoodpete

Senior Member
Code:
#Terminal 4800
Symbol TIME = w1
main:
	do
		high LED
		pause TIME
		low LED
		pause TIME
		
		if TIME < 500 Then
			inc TIME	
		Else If TIME > 500 Then
			dec TIME
		End If
		SerTxd(#TIME, " ")
	loop
I'm not giving you the answer. Just giving you a way of diagnosing the problem. I assume the your variable "TIME" is a word. 500 is too big for a byte.
 

ChedderCheeser

Senior Member
Code:
#Terminal 4800
Symbol TIME = w1
main:
	do
		high LED
		pause TIME
		low LED
		pause TIME
		
		if TIME < 500 Then
			inc TIME	
		Else If TIME > 500 Then
			dec TIME
		End If
		SerTxd(#TIME, " ")
	loop
I'm not giving you the answer. Just giving you a way of diagnosing the problem. I assume the your variable "TIME" is a word. 500 is too big for a byte.
well.. I have tried about 7 different re-codes, and I cant get it workin. 500 too large? How big can it be?
 

ChedderCheeser

Senior Member
Code:
#Terminal 4800
Symbol TIME = w1
main:
	do
		high LED
		pause TIME
		low LED
		pause TIME
		
		if TIME < 500 Then
			inc TIME	
		Else If TIME > 500 Then
			dec TIME
		End If
		SerTxd(#TIME, " ")
	loop
I'm not giving you the answer. Just giving you a way of diagnosing the problem. I assume the your variable "TIME" is a word. 500 is too big for a byte.
Ok.. heres this:
Code:
main:
	#terminal 4800
	symbol TIME = w1
	symbol ONN = w2
	let ONN = 1
	let TIME = 1

	do
		high 4
		pause TIME
		low 4
		pause TIME
		
		
		if ONN = 1 Then
			if TIME < 250 Then
				inc TIME
			End If
		Else If ONN = 0 Then	
			If TIME > 250 Then
				dec TIME
			End If
		End If
		
		If TIME = 250 Then
			If ONN = 1 Then
				let ONN = 0
			End If
		End If
		
		If TIME = 0 Then
			If ONN = 0 Then
				let ONN = 1
			End If
		End If
		
		
		
		SerTxd(#TIME, " ")
	loop
Hmmm... Any comments? Looks good... but it does NOT work. I cant figure it out dude... please help!! :D It shouldnt be THAT hard and it shoulnt take THAT much code! :D PLEASE REPLY ASAP! THX!!!
 

ChedderCheeser

Senior Member
Ok.. heres this:
Code:
main:
	#terminal 4800
	symbol TIME = w1
	symbol ONN = w2
	let ONN = 1
	let TIME = 1

	do
		high 4
		pause TIME
		low 4
		pause TIME
		
		
		if ONN = 1 Then
			if TIME < 250 Then
				inc TIME
			End If
		Else If ONN = 0 Then	
			If TIME > 250 Then
				dec TIME
			End If
		End If
		
		If TIME = 250 Then
			If ONN = 1 Then
				let ONN = 0
			End If
		End If
		
		If TIME = 0 Then
			If ONN = 0 Then
				let ONN = 1
			End If
		End If
		
		
		
		SerTxd(#TIME, " ")
	loop
Hmmm... Any comments? Looks good... but it does NOT work. I cant figure it out dude... please help!! :D It shouldnt be THAT hard and it shoulnt take THAT much code! :D PLEASE REPLY ASAP! THX!!!
this message^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 

darb1972

Senior Member
this message^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Hi Chedder

I think the lads are trying to steer you towards reading parts of the manuals. It's worth spending the time studying the material.

Personally I would'nt use the word "time" as a variable as it is a special function register in some PICAXE IC versions. It's best to get into the habit of only using valid labels.

Search the word "time" in second manual.
 

inglewoodpete

Senior Member
OK. The time variable (we'll call it wTime for uniqueness and clarity) Starts with a value of 0 and, because it is < 500, steps up to 499 - 1 step per loop. If you use my code, you will see the in the terminal window.

When wTime = 499 it increments to 500. Cool.

The question: What happens when the execution enters your If-then strucure with wTime = 500?
 

srnet

Senior Member
As inglewoodpete has said, think about what happens when the 'Time' variable reaches 500 ..........
 

hippy

Ex-Staff (retired)
Hmmm... Any comments? Looks good... but it does NOT work. I cant figure it out dude... please help!! :D It shouldnt be THAT hard and it shoulnt take THAT much code! :D PLEASE REPLY ASAP! THX!!!
Have you tried running it through simulation to see what happens ? You can reduce the 250 value to 25, even 10 or 5 while testing so it doesn't take so long to step round the code and see what is happening.

Alternatively you can add additional SERTXD or DEBUG commands within the code so you can trace where the code is going. When you see something happening that is wrong you can 'zoom out' mentally and hopefully figure out why the code got there, work out what caused that.

Your posts were between 03:30 and 07:00 GMT when most Brits, including myself and Technical, would normally be in bed or at least not looking at the forum; srnet is obviously an early riser plus we are fortunate in having an active international membership but there will always be times when response times will be longer.

It also helps yourself to help others to help you; so rather than "doesn't work!" it's best to say what results you get, indicate what it does wrong or similar. That will help people focus in on what could be causing that without having to run the code for themselves. The easier you can make it for others the more likely they are to help. Don't forget that if code looks right to you it may equally look right to others and they may not have the time to investigate more deeply.

Though I haven't run the code it looks like you probably adjust your numbers in a way that the conditional change isn't happening so the 'time' value gets stuck.

Two examples in the following post which show two different ways of doing a increasing and decrementing time period, and there's always FOR-NEXT as well.

The 'flash the LED' code moved to a subroutine for clarity, and once you've done that it can be easier to move away from a single loop to a multi-loop which can provide a simpler solution to a problems.
 
Last edited:

hippy

Ex-Staff (retired)
Code:
Do
  Gosub FlashLed
  If goingUp = 1 Then
    If time < 250 Then
      Inc time
    Else
      Dec time
      goingUp = 0
    End If
  Else
    If time > 0 Then
      Dec time
    Else
      Inc Time
      goingUp = 1
    End If
  End If
Loop
Code:
Do
  Do
    Gosub FlashLed
    Inc time
  Loop While time < 250
  Do
    Gosub FlashLed
    Dec time
  Loop While time > 0
Loop
 

ChedderCheeser

Senior Member
Code:
Do
  Gosub FlashLed
  If goingUp = 1 Then
    If time < 250 Then
      Inc time
    Else
      Dec time
      goingUp = 0
    End If
  Else
    If time > 0 Then
      Dec time
    Else
      Inc Time
      goingUp = 1
    End If
  End If
Loop
Code:
Do
  Do
    Gosub FlashLed
    Inc time
  Loop While time < 250
  Do
    Gosub FlashLed
    Dec time
  Loop While time > 0
Loop
Thanks! And to the other comments, thanks too. I should have been more patient and informative. I appreciate your feedback! Anyway, thanks all!
Heres what I have:
Code:
main:
	#Terminal 4800
	symbol wTIME = b0
	symbol LEDPin = 4
	output 4
	setfreq m4
	
	Do
	  Do
	    Gosub FlashLed
	    Inc wTIME
	  Loop While wTIME < 250
	  Do
	    Gosub FlashLed
	    Dec wTIME
	  Loop While wTIME > 0
	  SerTxd(#wTIME, " ")
	Loop
	End

FlashLed:
	high LEDPin
	pause wTIME
	low LEDPin
	pause wTIME
return
I dont get the serial code... it pops up, but doesnt show... i'm way confuesd... anyone? It does work as far as I can tell. I just want to be sure. :D
 
Last edited:

hippy

Ex-Staff (retired)
Hmmm... Well, it works so i can just screw it lol. Would be nice to know though. It doesnt work in simulate either. I guess thats probably what you're doing. :D
???

It's not clear at all what "it" is, what doesn't work, what you mean by "serial code". Maybe some enlightenment ?
 

ChedderCheeser

Senior Member
???

It's not clear at all what "it" is, what doesn't work, what you mean by "serial code". Maybe some enlightenment ?
Sorry bout that... in the serial terminal, I am SUPPOSED to see a bunch of numbers printing.. but see nothing. I do not NEED this, but it would be nice to make it work for testing purposes... as I will be adding to the code. Anyway, I hope I said that right... if not then sorry. :/ Anyway.. yeah. Lemme kno what you think.

Updated Code:

Code:
Init: ;DO NOT CHANGE
	Symbol wTIME = b0 ;DO NOT CHANGE
	Symbol minimum = b1 ;DO NOT CHANGE
	Symbol maximum = b2 ;DO NOT CHANGE
	Symbol LEDPin = 4 ;What pin is the LED on
	Let wTIME = 1 ;DO NOT CHANGE
	Let minimum = 1 ;What the time will start at 
	Let maximum = 100;What the time will go to
	Output 4 ;Make this the same as LEDPin
	Setfreq m4 ;m4 or m8. m8 is 2x speed.
	#Terminal 4800
	Goto Main

Main: ;DO NOT CHANGE
	Do ;DO NOT CHANGE
	  Do ;DO NOT CHANGE
	    Gosub FlashLed ;DO NOT CHANGE
	    Inc wTIME ;DO NOT CHANGE
	  Loop While wTIME < maximum ;DO NOT CHANGE
	  Do
	    Gosub FlashLed ;DO NOT CHANGE
	    Dec wTIME
	  Loop While wTIME > minimum ;DO NOT CHANGE
	  SerTxd(#wTIME, " ")
	Loop ;DO NOT CHANGE
	End ;DO NOT CHANGE
		

FlashLed:;DO NOT CHANGE
	High LEDPin ;DO NOT CHANGE
	Pause wTIME ;DO NOT CHANGE
	Low LEDPin ;DO NOT CHANGE
	Pause wTIME ;DO NOT CHANGE
Return ;DO NOT CHANGE
 
Last edited:

inglewoodpete

Senior Member
The #Teminal ... directive causes the terminal window to pop up immediately after you have downloaded your code to the PICAXE (the 4800 sets the baud rate - serial speed). At other times you need to press the F8 key if you want the terminal window.

The terminal window will show what is output from any SerTxd commands in your code. No output means the SerTxds are never being executed (bypassed?)

4800baud is the default SerTxd speed for a 4MHz 08M. If you rev it up with SetFreq m8, than you need to change the terminal speed to 9600baud.
 

westaust55

Moderator
Have a look at this adjusted code:

Notice that I have also moved a few lines around near the top such as SYMBOL statements and PE directives which are not part of the code actually downloaded into the PICAXE chip.
IMHO it is good to assign and keep such lines near the top for ease of finding in larger programs

The bit that really counts is the GOSUB Display and the actual small Display routine for the reasons IWP has intimated above

Code:
; conpiler directive - not sent to PICAXE
	#Terminal 4800

;Define variable alias names
	Symbol wTIME = b0 ;DO NOT CHANGE
	Symbol minimum = b1 ;DO NOT CHANGE
	Symbol maximum = b2 ;DO NOT CHANGE
	Symbol LEDPin = 4 ;What pin is the LED on

Init: ;DO NOT CHANGE
	Setfreq m4 ;m4 or m8. m8 is 2x speed
	Output LEDPin ;Make this the same as LEDPin
	
	Let wTIME = 1 ;DO NOT CHANGE
	Let minimum = 1 ;What the time will start at 
	Let maximum = 100;What the time will go to

	; Goto Main - not needed as the program will always "drop through" into the Main program section

Main: ;DO NOT CHANGE
	Do ;DO NOT CHANGE
	  Do ;DO NOT CHANGE
	    Gosub FlashLed ;DO NOT CHANGE
	    Inc wTIME ;DO NOT CHANGE
	    GOSUB Display
	  Loop While wTIME < maximum ;DO NOT CHANGE
	  
	  Do
	    Gosub FlashLed ;DO NOT CHANGE
	    Dec wTIME
	    GOSUB Display
	  Loop While wTIME > minimum ;DO NOT CHANGE
	  
	 
	Loop ;DO NOT CHANGE
	End ;DO NOT CHANGE
		

FlashLed:;DO NOT CHANGE
	High LEDPin ;DO NOT CHANGE
	Pause wTIME ;DO NOT CHANGE
	Low LEDPin ;DO NOT CHANGE
	Pause wTIME ;DO NOT CHANGE
Return ;DO NOT CHANGE

Display:
	    SerTxd(#wTIME, " ")
	    RETURN
 
Last edited:

inglewoodpete

Senior Member
WA55: We seem to have a tag team going here;). CC should be tucked up in bed now so I'm going to prune some rose bushes. I'll leave you on the 'night shift' (day shift in Australia):):)

Hmm that's post number 1999. I should get out more.
 

hippy

Ex-Staff (retired)
Sorry bout that... in the serial terminal, I am SUPPOSED to see a bunch of numbers printing.. but see nothing
That is odd. Try this ...

#Terminal 4800
Do
SerTxd( "> UUUUU <", CR, LF )
Pause 1000
Loop

You don't seem to say what PICAXE you are using so you may need #TERMINAL 9600 rather than 4800.
 
Top