For review. Finished I hope.

bull10x

Member
For review.

The program works great. I was wondering how to keep it functioning the same but maybe clean up the code any hints would be great.

Code:
#picaxe14m
symbol UVLEDTop = 0
symbol LEDTop =1
symbol UVLEDBottom =2
symbol LEDBottom =3
symbol Motor = 4
symbol Buzzer = 5

Init: let b0 = 0

Main: 
	if pin3 = 1 then Add
	goto main
	
Add: 
	pause 100 ‘short delay
	if b0 = 4 then let b0 = 0 endif
	let b0 = b0 + 1

	gosub Buzz
	
	if b0 = 1 then goto Allon
	if b0 = 2 then goto Beat
	if b0 = 3 then goto Nobubbles
	if b0 = 4 then goto Alloff
	goto main

Buzz: 
	for b1 = 1 to b0 ; define loop for b0 times
	high buzzer ; switch on output 5
	pause 250 ; wait 0.25 seconds
	low buzzer ; switch off output 5
	pause 250 ; wait 0.25 seconds
	next b1 ; end of loop
	return

Allon:	
	Outpins = %00011111
	if pin3 = 1 then goto Add
	goto Allon


Beat: if pin3 = 1 then goto Add	
	high LEDTop		
	high LEDBottom
	pause 500
	if pin3 = 1 then goto Add
	high UVLEDTop
	high UVLEDBottom
	pause 500
	if pin3 = 1 then goto Add
	low UVLEDTop
	low LEDTop
	low UVLEDBottom
	low LEDBottom
	pause 100
	if pin3 = 1 then goto Add
	goto beat


Nobubbles:

	high UVLEDTop			
	high LEDTop
	high UVLEDBottom
	high LEDBottom
	low motor
	if pin3 = 1 then goto Add
	goto Nobubbles

		

Alloff:				
	Outpins = %00000000
	goto main
I think that I am finished with the schematic also. (Dippy, I think I made the relay better also)




Any more input would be great.

Thanks so much.
Doug H.
 

John O

Senior Member
The 7805 could do with 100nF capacitors as close as possible to the input and output pins in parallel with the 100uF caps.

John.
 

MartinM57

Moderator
So does the software work in conjunction with all the hardware components on a breadboard/pcb/whatever or just in the simulator? And none of the buzzers, motors and LEDs interfere with each other? That would be the ultimate proof of a good hardware design.

As you sprung on us recently, the environment is a (motor) bike?

In that case, it needs to be pointed out that your 2x 100uf caps and a 7805 is a poor design for an automotive powered power supply. You might (and probably will) get away with it, but it's not right - it won't be (+ve and -ve) transient proof and it's not reverse polarity proof. You should at least use an automotive-oriented regulator (LM2937 etc) and follow its data sheet circuit - and a "proper" power supply design is more like 8 components than 3.

Indication of a fusing/polyfusing strategy would be good also.

Other than that, it looks good :)
 

Dippy

Moderator
I think I asked about the 100uF on the other thread... I couldn't get to the reason for the selection. :confused:
He hasn't even included a voltage rating.

Always decouple the PICAXE +v --Gnd. Did I mention that before?


Is it actually going to be used from a vehicular supply?
You don't actually need an automotive rated regulator if you know what you are doing, but it helps. But don't think an 'automotive rated' regulator on it's own will be magically indestructible - read, read and more reading needed.

If it IS to be used on a vehicular supply you DEFINITELY need to modify that circuit. I certainly would NOT put that circuit onto my car.
This was definitely mentioned before but I never saw a precise response.

And, yes I agree, try and keep all these things in one post.
 

bull10x

Member
West,
You could have kept your project together and posted on the end of a previous thread such as:
http://www.picaxeforum.co.uk/showthread.php?t=18298
I did not keep the project together on the previous thread because the title of that thread was "When to count and when to add" I thought that I would get more critical insight if I started a new thread for review.


Why do you need a separate block of code just to increment a number?
The Add block I can understand

I read up on "on goto & on gosub, setint & branch"

I did try the code you posted the one you mentioned in this post an in another post . I could get them to work but not in the fashion that I need It.
It would scroll through all of the routines and would end in a stack error.


John,

The 7805 could do with 100nF capacitors as close as possible to the input and output pins in parallel with the 100uF caps.
I think that I might just hack the 12vdc to 5vdc
car voltage converter


Martin,

So does the software work in conjunction with all the hardware components on a breadboard/pcb/whatever or just in the simulator? And none of the buzzers, motors and LEDs interfere with each other? That would be the ultimate proof of a good hardware design.
No, I have not worked this up on the breadboard yet. I wanted it polished up as much as possible before I did.

it won't be (+ve and -ve) transient proof
I don't understand why there would be transient voltage if all grounds are common?

it's not reverse polarity proof
I think that the only way that there would be reverse polarity is if I did it. If so I would serve me right.

But I am thinking of the cheep cigaret charger voltage regulator to 5vdc. Cheep and easy.


Dippy,

I think I asked about the 100uF on the other thread... I couldn't get to the reason for the selection.
He hasn't even included a voltage rating.
I thought that a 100uf cap was needed between pin 8 & 9 for decoupling?

I dont know what volatage any of the caps should be:(

Is it actually going to be used from a vehicular supply?
Yes it will be going on a Motorcycle.
 

Dippy

Moderator
I take it your are new to electronics.
Capacitors have VOLTAGE and ripple ratings as well as their basic capacitance.
And capacitor performance varies with type , construction and dielectric.
Electrolytics can be damaged or destroyed by reverse polarity.


Transients are not 'nullified' by a common ground.
Transients are those noisy spikes that are generated by alternators, ignitions, starter motors etc.
They can go down and up and are sometimes high voltage and often high frequency.

And it can vary with vehicle as some lines have protection on them.

An HF pulse can go through some regulators without touching the sides.
Read up on choke/inductors , transient suppressors. Search on the Farnell website and you will find many options.

You need to include components that can absorb or divert these pulses.
Without these your regulator or the circuit could pop!!!

In ADDITION, you need to use things like CERAMIC capacitors either side of the regulator to DECOUPLE?BYPASS small HF spike/transients.
Decouple the PICAXE +ve -- Gnd.

Add a DIODE to protect against reverse polarity.
Add a fuse to protect upstream and downstream.

I've run out of time - come in Martin. Do him a drawing.
I could borrow an Automotive PSU design from work but I think it may confuse rather than assist as for baby circuits like this there is no need to go OTT.
 

John O

Senior Member
But I am thinking of the cheep cigaret charger voltage regulator to 5vdc. Cheep and easy.
Personally, I'm not a great lover of those things. Sometimes even their own instructions recommend that you start the engine BEFORE you plug in your 5v device, which is tantamount to admitting that they don't suppress transients very well.

Inductive devices can generate a high reverse voltage when the original energizing voltage is turned off - that's the reason for the diode you added to the output relay - in effect to quench the reverse voltage at source.

Personally, I don't see a problem with using a 7805 regulator provided you use good design technique and keep the connections to the capacitors short - ideally less than 1mm to the 100nF caps.

I thought that a 100uf cap was needed between pin 8 & 9 for decoupling?
I dont know what volatage any of the caps should be:(
I think you're probably confused over nF and uF. Decoupling capacitors across an IC power supply pins should be 100nF (= 0.1uF) ceramic.

I suspect the two capacitors in your 7805 circuit may have originally been 100nF. 7805 type circuits need BOTH a 100nF capacitor - to give a path for unwanted high frequency voltages to get shunted to ground - AND a larger electrolytic capacitor to act as a 'voltage resevoir' to smooth out sudden changes in the load current.

Ceramic capacitors (the 100nF ones) are capable of handing high voltages so don't worry about the voltage rating of those. The electrolytic capacitors need to have a voltage rating above but not too far above the circuit voltage. It depends on the expected ripple current in the circuit but, in practical terms, on the 12 volt side around 25 volt DC capacitors and on the 5 volts side around 16 volts DC rating would be reasonable ball-park figures, I think.


John.
 

srnet

Senior Member
Be aware that just because these evil transients do not actually damage your circuit, there can be more subtle effects.

Micros (such as PICAXE) for instance can crash, missread switches have erroneous outputs etc.
 

Pauldesign

Senior Member
Hey Bull,

Enough have been said on caps; so i'll say something else based on your schematic in post (not so sure of your application and pre).

- What is the power rating (voltage and current) of your PSU and other components. (always state these parameters, if u want your circuit to be properly reviewed)
- In future please label your resistors to ease explanation.
- The connection of PB1 can be improved and hence eliminates one resistor.
- Why connecting +12V to LS2 (i assume it's a speaker;NEVER EVER!)?
- Why using 5 diodes in parallel and connecting to a single output pin?
- What colour of LED are you using; note the different their colour the
different their electrical properties and in your topology; always ensure
they're all of the same colour (in other words, don't mixed different colour
LEDs as they've different Vf voltages)?
- Assuming all the LEDs are red and powered by 12V; you'll need but 680
Ohms res by using voltage divider rule. Note, the
higher the res value, the more noisy it becomes; so i'll reckon u instead
used 330 Ohms and supply with 5V.
- Note, all inductive loads must always have a free wheeling diode; so also add one
to the motor (M1).-
- What value and voltage rating of cap is C1?

Those are my suggestions and the choice is yours.
 

westaust55

Moderator
West,

I did not keep the project together on the previous thread because the title of that thread was "When to count and when to add" I thought that I would get more critical insight if I started a new thread for review.



The Add block I can understand

I read up on "on goto & on gosub, setint & branch"

I did try the code you posted the one you mentioned in this post an in another post . I could get them to work but not in the fashion that I need It.
It would scroll through all of the routines and would end in a stack error.
I posted in the other thread with
ON b0 GOSUB Allon, Beat, Nobubbles, Alloff​

Your stack error comes about by virtue of the fact that you have a GOTO at the end of the sub-code blocks.
Change those GOTO's at the end of the code blocks to RETURN commands
or
change the
ON b0 GOSUB Allon, Beat, Nobubbles, Alloff​
to
ON b0 GOTO Allon, Beat, Nobubbles, Alloff​
 

vttom

Senior Member
Well, this might not be exactly right, but it's food for thought:

Code:
#picaxe14m
symbol UVLEDTop = 0
symbol LEDTop =1
symbol UVLEDBottom =2
symbol LEDBottom =3
symbol Motor = 4
symbol Buzzer = 5

Init: let b0 = 0

Main:	do
	loop until pin3 = 1

	' Add
	pause 100 ‘short delay
	b0 = b0 + 1 AND %00000011

	' Buzz
	for b1 = 0 to b0	' loop b0 times
		high buzzer	' switch on output 5
		pause 250	' wait 0.25 seconds
		low buzzer	' switch off output 5
		pause 250	' wait 0.25 seconds
	next b1


	select case b0
	
	case 0

		' Allon
		Outpins = %00011111

	case 1
	
		' Beat
		do
			if pin3 = 0 then
				high LEDTop		
				high LEDBottom
				pause 500
			endif

			if pin3 = 0 then

				high UVLEDTop
				high UVLEDBottom
				pause 500
			endif

			if pin3 = 0 then
				low UVLEDTop
				low LEDTop
				low UVLEDBottom
				low LEDBottom
				pause 100
			endif

		loop until pin3 = 1

	case 2
	
		' Nobubbles
		high UVLEDTop			
		high LEDTop
		high UVLEDBottom
		high LEDBottom
		low motor

	case 3
	
		'Alloff
		Outpins = %00000000

	endselect

	goto main
 

westaust55

Moderator
If you do not want the code to loop through all modes if the push button is held down, you need to further enhance the code by:
1. Adding a debounce
2. Checking that the switch is released (not just A contact bounce) before advancing to the next loop of the main code block
 

john2051

New Member
Why are there so many discrepancies in the bypass caps for the 7805.
The datasheet i have states a minimum configuration of a 330nf capacitor on the input and a 100nf cap on the output. It does say to minimize ripple that additional capacitors can be used.
It does get confusing why there are all these different combinations. Some people dont seem to use them at all.
Could we have some idea of what is an acceptable minimum config, and what would constitute a bomb proof belt an braces approach.

Thanks and regards....John
 

srnet

Senior Member
Why are there so many discrepancies in the bypass caps for the 7805.
Because there are many different designs of 7805.

Each manufacrturer will have their own recommendations for the capacitors, which they will have found as a result of testing their device.

The best, and correct advice, is to follow the recommendations on the datasheet for the actual device you are using, anything else is guesswork.
 

westaust55

Moderator
As srnet suggests there are variances between manufacturer’s requirements. For example:

The Fairchild datasheet does indicate 0.33uF on the input and 0.1uF on the output.
http://www.datasheetcatalog.org/datasheets/228/390068_DS.pdf

as does the KEC datasheet:
http://www.datasheetcatalog.org/datasheets/400/249955_DS.pdf


The ON Semiconductor datasheet (http://www.onsemi.com/pub_link/Collateral/MC7800-D.PDF) is slightly different and indicates:
A common ground is required between the input and the output voltages. The input voltage must remain typically 2.0 V above the output voltage even during the low point on the input ripple voltage.

Cin is required if regulator is located an appreciable distance from power supply
filter.
Cout is not needed for stability; however, it does improve transient response. Values of less than 0.1 uF could cause instability.
The National Semiconductors datasheet: http://www.national.com/ds/LM/LM340.pdf
Indicates 0.22uF for the input side and again has the note for the output side stating:
Although no output capacitor is needed for stability, it does help transient response. (If needed, use 0.1 μF, ceramic disc).
 
Last edited:

John O

Senior Member
Quoting from the ON Semiconductor datasheet....

Cout is not needed for stability; however, it does improve transient response. Values of less than 0.1 uF could cause instability.
.. which, as you've highlighted, doesn't make any sense at all. "Cout is not needed for stability but values less than 0.1uF could cause instability" . It has to be one or the other.

Although following the specific manufacturer's datasheet is obviously the best advice, in practice there must be hundreds of thousands of published circuits which use 'generic' component values. For example, the Picaxe manual 1 page 26 gives 'generic' values for a regulated power supply without specifying a particular manufacturer.

The reality is that most suppliers will send what they happen to have in stock anyway and will just advertise it as '7805'.

I'd suggest that 100nF on both input and output would be suitable for most situations. The electrolytics' values can be determined with a bit of common sense. The input fed from a solar-charged 12v lead acid battery is likely to need a smaller value ( C and V) than if fed from a cheapo wall adapter.

John.
 

Pauldesign

Senior Member
Hey John,

Why are there so many discrepancies in the bypass caps for the 7805.
In general, manufacturers usually provide a formula in the data sheet for calculating caps min value and voltage ratings. If both values are correctly calculated; any cap (prescribed type of cap) value x10 bigger than the calculated cap value (min cap value) and whose cap voltage rating is x2 bigger than cap calculated voltage rating (min cap voltage rating) can be used.

Too big a value too bulky and also the longer it takes to discharge when power is turned OFF and can be a problem.
 

westaust55

Moderator
Also, too large a capacitor value on the regulator output side (eg thousands of uF) will slow the rise time for the voltage on power up and can cause some logic not to initialise properly.
Then some folks may, for example, wonder why their LCD is not working.
 

Dippy

Moderator
You SHOULD use the values specified byt he Manuf , or as Paul says calaculted using an formula provided by the manufacturer.

Read The DATA SHEET for the device that YOU are using.
In fairness you can usually get away with s wide variety with linears.

There several reasons why you shouldn't have a monster on the O/P.
1. As westy says.
2) As Paul says
3) as I say; if Vout stays higher than Vin it can pop some regulators. Dunno about that old dinosaur though.

Design is often a slight combo of Manuf's calcs and your actual App.
If you have little experience then stick with Manuf's data. That is the best starting-off point.
Read first... breadboard second.
 

john2051

New Member
Thanks for all the replies...

It also seems that some manufacturers have problems also. One company selling micro products was having problems with the 7805 going into destruct mode, and taking other ics with it. Their recommendation was a 'largish'?? capacitor on the input..
There should be a 'suits all' set of components. I have 7805s from various manufacturers, maybe time to do some experimenting. I do know years ago we managed to get a motorola equivalent oscillating at above 10MHz!

Thanks again..regards john
 

bull10x

Member
I have changed the code so that I do not have "b0 goto" each process but now the program will skip the first gosub BUZZ and the goto ALLON

In simulation it will with one button press buzz once and then goto Beat

When it should with one button press buzz once and goto ALLON

In simulation it seems that b0 is set at 1 rather than 0.

So in simulation on the fourth button press it buzzes three times and goes to ALLOFF and that should be four buzzes and all off.

Code:
#picaxe14m
symbol UVLEDTop = 0
symbol LEDTop =1
symbol UVLEDBottom =2
symbol LEDBottom =3
symbol Motor = 4
symbol Buzzer = 5

Init: let b0 = 0

Main: 
	if pin3 = 1 then Add
	goto main
	
Add: 
	pause 2000 ‘short delay
	if b0 = 4 then let b0 = 0 endif
	let b0 = b0 + 1

	gosub Buzz


	ON b0 GOSUB Allon, Beat, Nobubbles, Alloff
	GOTO Main
	IF pin3 = 1 THEN
	INC b0
	b0 = b0 // 4 ; this will scroll through 0, 1, 2, 3 
	ENDIF


Buzz: 
	for b1 = 1 to b0 ; define loop for b0 times
	high buzzer ; switch on output 5
	pause 250 ; wait 0.25 seconds
	low buzzer ; switch off output 5
	pause 250 ; wait 0.25 seconds
	next b1 ; end of loop
	
	return

Allon:	
	Outpins = %00011111
	if pin3 = 1 then goto Add
	goto Allon


Beat: if pin3 = 1 then goto Add	
	high LEDTop		
	high LEDBottom
	pause 500
	if pin3 = 1 then goto Add
	high UVLEDTop
	high UVLEDBottom
	pause 500
	if pin3 = 1 then goto Add
	low UVLEDTop
	low LEDTop
	low UVLEDBottom
	low LEDBottom
	pause 100
	if pin3 = 1 then goto Add
	goto beat


Nobubbles:

	high UVLEDTop			
	high LEDTop
	high UVLEDBottom
	high LEDBottom
	low motor
	if pin3 = 1 then goto Add
	goto Nobubbles

		

Alloff:				
	Outpins = %00000000
	goto main
Any Ideas?


Thanks,
Doug
 

bull10x

Member
Whops code change I had
Code:
ON b0 GOSUB Allon, Beat, Nobubbles, Alloff
Needed to be
Code:
ON b0 goto Allon, Beat, Nobubbles, Alloff
Still have the same broblem though?

Code:
#picaxe14m
symbol UVLEDTop = 0
symbol LEDTop =1
symbol UVLEDBottom =2
symbol LEDBottom =3
symbol Motor = 4
symbol Buzzer = 5

Init: let b0 = 0

Main: 
	if pin3 = 1 then Add
	goto main
	
Add: 
	pause 2000 ‘short delay
	if b0 = 4 then let b0 = 0 endif
	let b0 = b0 + 1

	gosub Buzz


	ON b0 GOto Allon, Beat, Nobubbles, Alloff
	GOTO Main
	IF pin3 = 1 THEN
	INC b0
	b0 = b0 // 4 ; this will scroll through 0, 1, 2, 3 
	ENDIF


Buzz: 
	for b1 = 1 to b0 ; define loop for b0 times
	high buzzer ; switch on output 5
	pause 250 ; wait 0.25 seconds
	low buzzer ; switch off output 5
	pause 250 ; wait 0.25 seconds
	next b1 ; end of loop
	
	return

Allon:	
	Outpins = %00011111
	if pin3 = 1 then goto Add
	goto Allon


Beat: if pin3 = 1 then goto Add	
	high LEDTop		
	high LEDBottom
	pause 500
	if pin3 = 1 then goto Add
	high UVLEDTop
	high UVLEDBottom
	pause 500
	if pin3 = 1 then goto Add
	low UVLEDTop
	low LEDTop
	low UVLEDBottom
	low LEDBottom
	pause 100
	if pin3 = 1 then goto Add
	goto beat


Nobubbles:

	high UVLEDTop			
	high LEDTop
	high UVLEDBottom
	high LEDBottom
	low motor
	if pin3 = 1 then goto Add
	goto Nobubbles

		

Alloff:				
	Outpins = %00000000
	goto main
 

hippy

Ex-Staff (retired)
Still have the same broblem though?
Not sure what the 'old problem' is, but if it's not changing mode as I'd guess, try following the code through by eye, or even simulate it.

Say you reach 'Nobubbles', it either stays in that loop or goes to 'Add'. Once at 'Add' it then reaches the 'On b0 Goto', and back to 'Nobubbles', before you check 'pin3'.
 

bull10x

Member
when I simulate it with " ON b0 GOTO Allon, Beat, Nobubbles, Alloff"

It will skip allon.

As if Beat was first in line .
 

hippy

Ex-Staff (retired)
That indicates b0 has value 1 rather than value 0. Seeing the b0=b0+1 before 'gosub buzz' that may be why.

I haven't followed the thread so don't understand what the code is meant to be doing or its problems but you should be able to determine what your code is doing by stepping through simulation.
 

hippy

Ex-Staff (retired)
If there's a b0=b0+1 which changes b0 from zero to one then it will do that whether before or after the 'gosub buzz'.

Single step the program and watch the value of b0, see where it changes and check its value when the 'on b0 goto' is executed.

The PICAXE / Simulator will do whatever you tell it to do in the program. What you tell it to do may not be right, which is the nature of "bugs" in programs; where you tell it to do something which isn't what you want it to be doing. "Debugging" is the process of finding where what it does diverges from what you want.
 

bull10x

Member
b0 is = 0 till the program hits b0=b0+1 then its value changes to 1 it will GOSUB BUZZ buzz once and then will porceed to BEAT never going to ALLON.

This is a bug in the simulator?

Thanks,
Doug
 

lbenson

Senior Member
Note that if you run the simulator, pause, click the ">>" button so you can see the variables, and single step with the "}" button, you will see that b0 is not equal to 0 when you expect it to go to Allon and it doesn't. If b0 is equal to 0, execution will jump to Allon.

You changed "gosub" to "goto". You (and others) will probably find the program easier to follow if you change it back to "gosub" and fix the code-flow problem which caused it to fail. The use of "goto" notoriously complicates the understanding of program flow.

You can avoid "goto" altogether if you make your main routine like this:

Code:
main:
  do
   ' your code here
  loop

sub1:
  ' ...
  return

sub2:
  ' ...
  return
 

bull10x

Member
I don't understand.:confused:

Most everybody is saying to use GOSUB instead of GOTO but with GOSUB you need RETURN at the end of that part of the program such as ALLON, BEAT, etc.

When I want them to continue the loop until bo changes

So how is it more efficient to do this

Code:
Init: 
	let b0 = 0

Main: 
	if pin3 = 1 then Add
	goto main
	
Add: 
	pause 2000 ‘short delay
	if b0 = 4 then let b0 = 0 endif
	let b0 = b0 + 1

	gosub Buzz

	ON b0 GOSUB trick, Allon, Beat, Nobubbles, Alloff
	GOTO Main
	IF pin3 = 1 THEN
	INC b0
	b0 = b0 // 4 ; this will scroll through 0, 1, 2, 3 
	ENDIF
Insted of this???

Code:
Init: 
	let b0 = 0

Main: 
	if pin3 = 1 then Add
	goto main
	
Add: 
	pause 100 ‘short delay
	if b0 = 4 then let b0 = 0 endif
	let b0 = b0 + 1

	gosub Buzz
	
	if b0 = 1 then goto Allon
	if b0 = 2 then goto Beat
	if b0 = 3 then goto Nobubbles
	if b0 = 4 then goto Alloff
	goto main
In the first code I put in TRICK: pause 1 for a place holder for when b0=0 otherwise it would skip the first gosub.

Doug
 
Last edited:

lbenson

Senior Member
It's not more efficient in terms of code execution speed to use gosub instead of goto, but as programs become larger, it is much easier with "goto" to write "speghetti" code which no one else can follow, and which the original programmer will find it hard to follow if there is a need to return and modify the code.

Your use of the "trick" subroutine is one solution to the problem you were having, but a more elegant solution would be to figure how to code the problem without using a "trick". But not everyone finds elegance compelling (or even detectable), so you are free to use whatever works and works for you.
 
Top