By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

Using Visual Basic to control a lighting dimmer

P: 6
Hi, I'm quite new to all this so please forgive my lack of technical description.

Project background:
Basically I have had the vision of creating a home lighting control system for the last 3 years. This year I have been trying really hard to get a prototype model working.

I need to work out how to use visual basic to 'fade' to a desired figure in a number of seconds. From there I can convert the figure into an analogue voltage (0 - 10v) using a USB interface card and then control my lighting dimmer.

In simple terms, if I refer to the level of light output in percent, if I want to go from 0% to 100% in 5 seconds it would fade off to fully on in 5 secs. Likewise if I want to go from 70% to 30% in 3 secs it would do so. I did have an example given to me using a timer and a VScroll bar, but somewhere along the lines it gets confused.

Sorry about the long description but I'm really eager to get this thing working!

If anyone can help me in any way I will be very grateful.

Adam
Jan 21 '08 #1
Share this Question
Share on Google+
11 Replies


lotus18
100+
P: 866
I did have an example given to me using a timer and a VScroll bar, but somewhere along the lines it gets confused.
Can you post your codes for the reference of our experts here ^ ^

Rey Sean
Jan 21 '08 #2

Expert 5K+
P: 8,434
What version of VB are you using? Timers and events and things work quite differently depending on the version.

All in all though, it sounds like quite a cool idea, and not too difficult.
Jan 21 '08 #3

daniel aristidou
100+
P: 491
Think about it logically......if you need to increase from 0% to 100% in 5 seconds. for each Second you increase by 20%.

So if you set your timer to 1000milliseconds.....Equivalent of 1 second
Set your progress bar to a max value of 100
Just add 20 to the progress bar on each loop
For example:

Expand|Select|Wrap|Line Numbers
  1. Timer1_tick
  2. 'Use an if statement to ensure that the progress bar value is less that 100%
  3. If Progressbar.value = Progressbar.max then
  4.   'Disables the timer
  5.   Timer1.enabled = False
  6. Else
  7.   'Sets The progress bar equal to its own value plus the additional 20
  8.   Progressbar.value = Progressbar.value + 20
  9. End if
  10. End sub
for a smoother increase of the value.
use a time of 500 milliseconds and a increase of 10 per loop
or 250 and 5
Jan 21 '08 #4

Expert 5K+
P: 8,434
I decided this would be a fun project to while away part of the evening. So here's the little sample I threw together. I'll zip up the (VB6) project and EXE, and post the form code here. It's just a single form, pretty simple really.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. DefLng A-Z
  3.  
  4. Dim sngStart As Single        ' The starting value (0-100)
  5. Dim sngEnd As Single          ' The ending value (0-100)
  6. Dim sngSpeed As Single        ' How fast to change (0-10)
  7.  
  8. Const const_SWITCHRATE As Long = 100 ' How many times to "tick" per second.
  9. Dim sngIncrement As Single    ' Internal - how much to change each "tick".
  10. Const const_MINIMUM As Long = 20 ' Start/End must be at least this different.
  11.  
  12.  
  13.  
  14. Private Sub Form_Load()
  15.   UpdateLabels
  16. End Sub
  17.  
  18. Private Sub scrlStart_Change()
  19.   UpdateLabels
  20. End Sub
  21.  
  22. Private Sub scrlStart_Scroll()
  23.   UpdateLabels
  24. End Sub
  25.  
  26. Private Sub scrlEnd_Change()
  27.   UpdateLabels
  28. End Sub
  29.  
  30. Private Sub scrlEnd_Scroll()
  31.   UpdateLabels
  32. End Sub
  33.  
  34. Private Sub scrlSpeed_Change()
  35.   UpdateLabels
  36. End Sub
  37.  
  38. Private Sub scrlSpeed_Scroll()
  39.   UpdateLabels
  40. End Sub
  41.  
  42.  
  43. Private Sub UpdateLabels()
  44.   ' Set labels to display the current settings.
  45.   lbStart = scrlStart.Value
  46.   lbEnd = scrlEnd.Value
  47.   lbSpeed = scrlSpeed.Value
  48.   ' Set variables which will control the action.
  49.   sngStart = scrlStart.Value
  50.   sngEnd = scrlEnd.Value
  51.   sngSpeed = scrlSpeed.Value
  52.  
  53.   cmdGo.Enabled = (Abs(sngStart - sngEnd) >= const_MINIMUM)
  54.  
  55.   SetLightTo sngStart
  56.  
  57.   'DoEvents
  58. End Sub
  59.  
  60.  
  61.  
  62. Private Sub cmdGo_Click()
  63.   ' This is performed when the Go! button is clicked, to start the sequence.
  64.   sngIncrement = (sngEnd - sngStart) / sngSpeed / const_SWITCHRATE
  65.   Timer1.Interval = 1000 / const_SWITCHRATE
  66.   Timer1.Enabled = True
  67.   fmSettings.Enabled = False
  68. End Sub
  69.  
  70. Private Sub Timer1_Timer()
  71.   ' This is fired 'n' times per second to adjust the light, once Go! clicked.
  72.  
  73.   ' Just modify the brightness by the appropriate amount, then
  74.   ' check whether we've reached the ending value.
  75.   ' The reason for the comlicated IF is that when floating point numbers
  76.   ' are stored in binary form, they lose precision. So instead of 100, you
  77.   ' may end up with something like 100.7878. So a straight IF X = Y test
  78.   ' fails, and it keeps going for ever.
  79.   sngStart = sngStart + sngIncrement
  80.   SetLightTo sngStart
  81.   If (sngIncrement > 0 And sngStart >= sngEnd) _
  82.   Or (sngIncrement < 0 And sngStart <= sngEnd) Then
  83.     Timer1.Enabled = False
  84.     fmSettings.Enabled = True
  85.     ReadCurrentSetting
  86.     UpdateLabels ' To reset sngStart and so on.
  87.     Beep
  88.   End If
  89. End Sub
  90.  
  91.  
  92. Private Sub SetLightTo(ByVal sngNewSetting As Single)
  93.   ' This sub sets the light to the specified brightness (0-100)
  94.   shapLight.BackColor = RGB(sngNewSetting * 2.55, sngNewSetting * 2.55, 0)
  95.   DoEvents
  96. End Sub
  97.  
  98. Private Sub ReadCurrentSetting()
  99.   ' Adjust the user settings to match the light.
  100.   scrlStart = sngStart
  101. End Sub
Attached Files
File Type: zip Dimmer Sample.zip (5.7 KB, 162 views)
Jan 21 '08 #5

P: 6
Hey guys,

Thanks ever so much, alot of positive answers. I will have a go at the examples you have provided when i get chance maybe later tonight! will let you know how i get on!

Many thanks again

Adam
Jan 21 '08 #6

Expert 5K+
P: 8,434
Cool! Let us know how you get on with the project.
Jan 21 '08 #7

P: 6
Hi, i tried the example given to me by `killer42' and i added in a bit of code so i could use my interface board. I got it all working fine, but the times seems a bit off... eg 5secs seems to be around 8 secs.. has anybody got any clues? Also if i were to replicate the controls frame to control multiple dimmers would i need to use more than 1 timer? Apart from that i'm one big step closer to cracking this project!

Thanks

Adam
Jan 23 '08 #8

Expert 5K+
P: 8,434
Hi, i tried the example given to me by `killer42' and i added in a bit of code so i could use my interface board. I got it all working fine, but the times seems a bit off... eg 5secs seems to be around 8 secs.. has anybody got any clues?
A couple of possibilities come to mind...
  • My calculations may have been way off. I didn't test the timing at all carefully. The "10 second" setting seemed longer than the "5 second" setting, so I was happy with that. You might want to double-check my logic.
  • Perhaps there's some delay in the response. For instance, perhaps you're telling the board to adjust the light 100 times a second, but it takes a tenth of a second for the light to respond. So the light might still take a few seconds to finish changing brightness after the code finishes.
  • A lot depends on how the interface works. Is it synchronous? When my code calculates how much to adjust the light on each "tick" of the timer, I suppose it assumes that each tick will finish more or less instantaneously. You might need to adjust either the increment amount, or (more sensibly) the logic.
    This sample code is only intended to provide some ideas. Your logic may end up quite different. For instance, rather than adjusting by a fixed amount each tick, you might work out how far along the delay period you have reached, and thus what the current setting should be. Then set it accordingly. This would allow for delays - if something slowed you down, you might just notice a faster jump in brightness when it catches up.
  • Um...

Also if i were to replicate the controls frame to control multiple dimmers would i need to use more than 1 timer?
I'd say that you can easily drive everything off a single timer. Whether it's appropriate I couldn't say. One thing - I learned early in my VB "career" to avoid creating more timers than necessary, as (A) there was a limit to how many you could use, and (B) they tended to be a bit "expensive". I don't know whether these still apply.

Just try to make it as modular and expandable as possible, so you can easily add more components (lights, or whatever). Before you know it, we'll be seeing your software on the shelves at Woolworths. :)
Jan 24 '08 #9

Ali Rizwan
100+
P: 927
How u have done this?
How have u controled the bulb using this?
How u make conectivity between PC and bulb?
Can u plz tell me or send me the code.

Thanx
Ali
Jan 24 '08 #10

P: 6
How u have done this?
How have u controled the bulb using this?
How u make conectivity between PC and bulb?
Can u plz tell me or send me the code.

Thanx
Ali

This maybe something i missed out earlier in the topic list. I'm using a simple usb interface board to prototype with. It allows the control of digital and analogue inputs and outputs. Basically i'm using the analogue outputs to provide the communication with my dimmers. The dimmers use a 0-10v system, 0v = no output, 5V = half output, and 10v = full (approx), or anything in between. The dimmers are then connected to incandescent lighting. The interface board allows control through visual basic coding using DLL(s) (Dynamic Link Library(s)) to control the inputs/outputs. I have used large chunks of the code "Killer42" posted (for which i am extremely gratefull) to create a working prototype.

Thankyou for everybody who has helped to provide answers to my on going 'headache'.

If anybody has any questions about my project so far, i will be more than willing to try and help and to explain it.

Thanks,
Adam
Jan 24 '08 #11

Expert 5K+
P: 8,434
Hm, sounds like a lot of fun. I might have a look at some interface stuff like that some day.

Anyway, when I'm reading about all your awards in a couple of years, I hope I'll have a footnote somewhere. :)




P.S. My people will be in touch to discuss royalties, soon afterward. :D
Jan 25 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.