472,982 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,982 software developers and data experts.

Using Visual Basic to control a lighting dimmer

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.

Jan 21 '08 #1
11 3037
866 512MB
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
8,435 Expert 8TB
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
491 256MB
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
8,435 Expert 8TB
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
  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)
  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.
  14. Private Sub Form_Load()
  15.   UpdateLabels
  16. End Sub
  18. Private Sub scrlStart_Change()
  19.   UpdateLabels
  20. End Sub
  22. Private Sub scrlStart_Scroll()
  23.   UpdateLabels
  24. End Sub
  26. Private Sub scrlEnd_Change()
  27.   UpdateLabels
  28. End Sub
  30. Private Sub scrlEnd_Scroll()
  31.   UpdateLabels
  32. End Sub
  34. Private Sub scrlSpeed_Change()
  35.   UpdateLabels
  36. End Sub
  38. Private Sub scrlSpeed_Scroll()
  39.   UpdateLabels
  40. End Sub
  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
  53.   cmdGo.Enabled = (Abs(sngStart - sngEnd) >= const_MINIMUM)
  55.   SetLightTo sngStart
  57.   'DoEvents
  58. End Sub
  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
  70. Private Sub Timer1_Timer()
  71.   ' This is fired 'n' times per second to adjust the light, once Go! clicked.
  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
  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
  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, 187 views)
Jan 21 '08 #5
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

Jan 21 '08 #6
8,435 Expert 8TB
Cool! Let us know how you get on with the project.
Jan 21 '08 #7
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!


Jan 23 '08 #8
8,435 Expert 8TB
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
925 512MB
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.

Jan 24 '08 #10
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.


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.

Jan 24 '08 #11
8,435 Expert 8TB
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

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

Similar topics

by: Tech | last post by:
In Visual Basic 6.0, the Locked property of a ComboBox control determined whether the text-box portion of the control could be edited. In Visual Basic ..NET, the Locked property prevents a control...
by: Rob R. Ainscough | last post by:
MS Visual Studio Ad contained in VS Magazine. Two developers in "hip" clothing diagramming out a huge flow chart on a beach. I could NOT stop laughing at the stupidity of the ad -- "Let your...
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
by: ME | last post by:
I can't seem to delete records using an objectdatasource. The object is a Typed Collection built by Visual Studio that access a SQL Database. When I try to delete using the ObjectDataSource it...
by: MikeB | last post by:
I've been all over the net with this question, I hope I've finally found a group where I can ask about Visual Basic 2005. I'm at uni and we're working with Visual Basic 2005. I have some books, ...
by: vidhyaprem | last post by:
i have started a small project in visual basic regarding the stock details in our textile company.It has details like product id,product name,no of items,product picture etc.. so i have created a...
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code,...
by: Nathan Sokalski | last post by:
I am attempting to create icons for controls I have created using VB.NET by using the System.Drawing.ToolboxBitmap attribute. I have managed to do this in C# by specifying the path to the *.ico...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.