Auto Screen Brightness control system in C#

A place to showcase LattePanda creations!
Post Reply
User avatar
Kellman616
Posts: 42
Joined: Fri Feb 24, 2017 7:11 am

Tue Feb 06, 2018 3:18 am

Image

Your phone is smart enough to adjust the screen brightness automatically. But most of the computers don't have this sweet function. :? In order to make my LattePanda more humanized, I write an easy demo to do this. Hope you like it! If you have any questions, please let me know:-)

Step 1: What You Need

Hardware list:
Software list:
Note:LattePanda is a low cost regular Windows computer, it also includes an Arduino co-processor, which means it can be used to control and sense the physical world when you add sensors and actuators.If you don't have the LattePanda, you can plug in Leonardo to your computer instead.

DEMO 1: Simple Brightness Control Demo

Image

First of all, let's try to use an easy way to change the screen brightness like the gif picture show above. You just need to use your computer.

1.Open the Visual Studio and create a new Windows Forms Application.

2.In 'ToolBox' dialog, drag the 'Button' and 'Trackbar' to the form.

Image

3.Copy the following code.

Code: Select all

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 
namespace brightnesscontrol 
{ 
   public partial class Form1 : Form 
   { 
       [DllImport("gdi32.dll")] 
       private unsafe static extern bool SetDeviceGammaRamp(Int32 hdc, void* ramp); 
       private static bool initialized = false; 
       private static Int32 hdc; 
       private static int a; 
       public Form1() 
       { 
           InitializeComponent(); 
       } 
       private static void InitializeClass() 
       { 
           if (initialized) 
               return; 
           hdc = Graphics.FromHwnd(IntPtr.Zero).GetHdc().ToInt32(); 
           initialized = true; 
       } 
       public static unsafe bool SetBrightness(int brightness) 
       { 
           InitializeClass(); 
           if (brightness > 255) 
               brightness = 255; 
           if (brightness < 0) 
               brightness = 0; 
           short* gArray = stackalloc short[3 * 256]; 
           short* idx = gArray; 
           for (int j = 0; j < 3; j++) 
           { 
               for (int i = 0; i < 256; i++) 
               { 
                   int arrayVal = i * (brightness + 128); 
                   if (arrayVal > 65535) 
                       arrayVal = 65535; 
                   *idx = (short)arrayVal; 
                   idx++; 
               } 
           } 
           bool retVal = SetDeviceGammaRamp(hdc, gArray); 
           return retVal; 
       } 
       private void trackBar1_Scroll(object sender, EventArgs e) 
       { 
       } 
       private void button1_Click(object sender, EventArgs e) 
       { 
           a = trackBar1.Value; 
           SetBrightness(a); 
       } 
   } 
} 
4. Start. Drag the bar and press the button.

DEMO 2: Auto Screen Brightness Control Demo

Image

If you have the Lattepanda, that's perfect. If you don't, you can use Arduino UNO or Lenardo board and connect it to your computer.

1. Open Arduino IDE and select the “StandardFirmata” ( File-Examples-firmata-StandardFirmata ).Upload the sketch.

Image

2. Connect the Ambient Light Sensor directly into pin A0.

Image

3. Open Visual Studio and create a new Console Application.

Image

4. Download the LattePanda.Firmata class library.Add the downloaded class library to your project. Open your Solution Explorer and right-click in the blank area, then add existing item→select arduino.cs (Get more help here).

Image

5. Copy the following code, press Start.

Code: Select all

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Threading; 
using LattePanda.Firmata; 
using System.Drawing; 
using System.Runtime.InteropServices; 
namespace screenbrightnessautocontrol
{ 
   class Program 
   { 
       [DllImport("gdi32.dll")] 
       private unsafe static extern bool SetDeviceGammaRamp(Int32 hdc, void* ramp); 
       private static bool initialized = false; 
       private static Int32 hdc; 
       private static int a; 
       private static void InitializeClass() 
       { 
           if (initialized) 
               return; 
           hdc = Graphics.FromHwnd(IntPtr.Zero).GetHdc().ToInt32(); 
           initialized = false; 
       } 
       public static unsafe bool SetBrightness(int brightness) 
       { 
           InitializeClass(); 
           if (brightness > 255) 
               brightness = 255; 
           if (brightness < 0) 
               brightness = 0; 
           short* gArray = stackalloc short[3 * 256]; 
           short* idx = gArray; 
           for (int j = 0; j < 3; j++) 
           { 
               for (int i = 0; i < 256; i++) 
               { 
                   int arrayVal = i * (brightness + 128); 
                   if (arrayVal > 65535) 
                       arrayVal = 65535; 
                   *idx = (short)arrayVal; 
                   idx++; 
               } 
           } 
           bool retVal = SetDeviceGammaRamp(hdc, gArray); 
           return retVal; 
       } 
       static Arduino arduino = new Arduino(); 
       static void Main(string[] args) 
       { 
           arduino.pinMode(0, Arduino.PWM); 
           SetBrightness(70); 
           int flag1 = 0; 
           int flag2 = 1; 
           int flag3 = 0; 
           int j; 
           while (true) 
           { 
               int a=arduino.analogRead(0); 
               Console.WriteLine(a);         
               if (a >= 100) 
               { 
                   if (flag1 == 1) 
                   { 
                       for (j = 0; j <= 140; j+=4) 
                       { 
                           Console.WriteLine(j); 
                           SetBrightness(j); 
                       } 
                   } 
                   if(flag2==1) 
                   { 
                       for(j=70;j<=140;j+=4) 
                       { 
                           SetBrightness(j); 
                           Console.WriteLine(j); 
                       } 
                   } 
                   flag1 = 0; 
                   flag2 = 0; 
                   flag3 = 1; 
               } 
               if((a > 25)&& (a < 100)) 
               { 
                   if (flag3 == 1) 
                   { 
                       for (j = 140; j >= 70; j-=4) 
                       { 
                           SetBrightness(j); 
                           Console.WriteLine(j); 
                           Thread.Sleep(1); 
                       } 
                   } 
                   if(flag1==1) 
                   { 
                       for (j = 0; j <= 70; j+=4) 
                       { 
                           SetBrightness(j); 
                           Console.WriteLine(j); 
                       } 
                   } 
                   flag1 = 0; 
                   flag2 = 1; 
                   flag3 = 0; 
               } 
               if(a<25) 
               { 
                   if(flag2==1) 
                   { 
                       for(j=70;j>=0;j-=4) 
                       { 
                           SetBrightness(j); 
                           Console.WriteLine(j); 
                       } 
                   } 
                   if (flag3 == 1) 
                   { 
                       for (j = 140; j >= 0; j-=4) 
                       { 
                           SetBrightness(j); 
                           Console.WriteLine(j); 
                       } 
                   } 
                   flag1 = 1; 
                   flag2 = 0; 
                   flag3 = 0; 
               } 
               Thread.Sleep(1000); 
           } 
       } 
   } 
} 
Image

Try to shine your flashlight over the sensor and see what will happen. The screen brightness will change according to the light intensity. Every second, the computer will read the data from the sensor and then judge the light intensity. I set 3 levels of screen brightness in my code.The screen will become brighter when the light intensity is high.
So far so good? If you have any questions, please let me know!

Problems You May Meet

Image

Solution:

Image

To use unsafe code blocks, the project has to be compiled with the /unsafe switch on. Open the properties for the project, go to the Build tab and check the Allow unsafe code checkbox.

Summary:

These two demo show how to control your computer screen brightness. Hope you like it. If you have any questions, please let me know.
If the light sensor could be integrate on the board, it would be better. :roll:

User avatar
LattePanda
Site Admin
Posts: 2425
Joined: Wed Jan 20, 2016 9:09 am

Fri Mar 02, 2018 11:09 am

Cool, isn't it? ;)
Enjoy Tinkering with LattePanda ! :lol:

User avatar
patrick.goina
Posts: 1
Joined: Sat Oct 27, 2018 7:44 pm

Sat Jan 05, 2019 10:13 am

hi guys, the program doesent work, can any one help me?
Thanks

User avatar
LattePanda
Site Admin
Posts: 2425
Joined: Wed Jan 20, 2016 9:09 am

Mon Jan 07, 2019 8:09 am

patrick.goina wrote:
Sat Jan 05, 2019 10:13 am
hi guys, the program doesent work, can any one help me?
Thanks
Dear Patrick, could u please share more details about your problem? Let's see if we can help or connect with our community contributors. ;)
Enjoy Tinkering with LattePanda ! :lol:

Post Reply