Pinout voltage change

alex126

Member
Hello. I have a question. I have the code with some cases and 2 parallel tasks. So, the pwm for fading doesn't work. So I thougth to play directly with the voltage of pinout. In general, what is the best way to change the voltage of pinout. Thank you.
 

lbenson

Senior Member
More information is needed about your intentions and your code. PWM certainly does work for fading (LEDs), so what exactly are you fading, what does your circuit look like, and what is your code?
 

alex126

Member
Let's say there is a B.4 with

High B.4 (it gives 1,5v)
Pause 100
Low B.4,
Pause 100
High B.4 (it gives 2,5v)

Is it possible by code without resistors. I looked at dacsetup. Not sure. Thank you
 

hippy

Technical Support
Staff member
There is no direct way to change the voltage of a digital output pin. The DAC ouput pin can have its voltage changed but that doesn't have enough oomph to drive a LED without adding extra external hardware.

PWMOUT would be the solution for LED brightness control and that will work in a program with two or more parallel tasks.

It would be best to resolve whatever the issue is which is preventing your program from doing what is required rather than trying to find other ways to do it.

If you could tell us what your program should be doing, how it is intended to control the LED, tell us how you have things wired, can post a photo of your hardware and your code, members here will be able to take a look at that and offer advice.
 

alex126

Member
There is no secret. My circuit Is just a bulky compromise between what I'd expected and what I've got. This is a smart candle which I posted with questions.
1 So there is a micro to blow to switch off the leds for some seconds (first task)
2 the leds
3 coil that moves the flame (second task)

- The fading effect to imitate the flame with pwm for the leds doesn't function. So I went to just high/low
It's here in: In case V_min to V_med
high y_led
pause 30
low y_led
pause 5
Now it's blinking till these numbers (pause 30 and 5) and below it goes off. But I tried to do the fading effect
Thank you

Here is the code:

#PICAXE 18M2
#NO_DATA

symbol V_micro = B.1
symbol d_val = b4

symbol y_led = B.3
symbol y_led_2 = B.2
symbol y_led_3 = B.5

symbol V_min = 0
symbol V_med = 35
symbol V_med2 = 100
symbol V_max = 256

main:
start0:
readadc V_micro, d_val
select d_val

case V_min to V_med ; y_led standard flickering. y_led_2 and y_led_3 go high
high y_led
pause 30
low y_led
pause 5
high y_led_2
high y_led_3
pause 25
goto start0

start1: ;Coil moves the flame in parallel
high B.4
pause 70
low B.4
pause 2000
high B.4
pause 50
low B.4
pause 200
high B.4
pause 50
low B.4
pause 500
goto start1

case V_med2 to V_max ; blowing and the leds switch off
low y_led
low y_led_2
low y_led_3
pause 10000
high y_led
high y_led_2
high y_led_3

case V_med to V_med2 ; blowing and the leds are blinking (flickering) for some seconds
gosub flsh
endselect

goto main

flsh:
for b2=1 to 15
high y_led
high y_led_2
high y_led_3
pause 30
low y_led
low y_led_2
low y_led_3
pause 30
next b2
return
 

Attachments

lbenson

Senior Member
The 18M2 has only 2 pwm pins--B.3 and B.6, so you couldn't use B.2 or B.5 for pwm fading.

Does this fade in and out for you with the LED on B.3?
Code:
#PICAXE 18M2
#NO_DATA

symbol y_led = B.3 ' pwm pin on 18M2
symbol y_led_2 = B.2
symbol y_led_3 = B.5

pwmout pwmdiv4, y_led, 249, 999 ' 1000Hz at 100% @ 4MHz
pause 1000
do
  w1 = 0
  pause 1000
  for b4=1 to 99
    w1 = w1 + 10
    pause 100
    pwmduty y_led, w1
  next b4
  for b4=1 to 99
    w1 = w1 - 10
    pause 100
    pwmduty y_led, w1
  next b4
loop
 

alex126

Member
Yes, the fading works like a single function which loops without interruption
When blowing in micro, this doesn't trigger the case V_med2 to V_max, which is the leds go off
Nor the case V_med to V_med2, which is the leds are blinking for some seconds
Thanks
 

Attachments

lbenson

Senior Member
I was just attempting to show that pwmout worked to increase and lessen the brightness of an LED. You'd need to adapt that to suit your requirements (noting that if you need to use different pwm duty cycles on 3 LEDs, you'd need a 14M2 or a 20M2). See Manual 1.
 

hippy

Technical Support
Staff member
I think part of the problem may be that the structure of the code is particularly odd -
Rich (BB code):
start0:
  select case
    case V_min to V_med
      goto start0
    start1:
      goto start1
    case V_med2 to V_max
    case V_med to V_med2
  end select
  goto main
Having the second task (start1) within a CASE option will probably work and the "goto start0" prevents the first task (start0) falling into it, but I am guessing it probably doesn't reflect what was intended, and makes it very hard to see what may have been intended.

I would suggest removing "Start1:" and its related code for now. Concentrate on getting the LED flickering and blowing stuff working without multiple tasks. Once that's working add the second task.
 
Top