C# TrackBar ScrollBar

Thread Starter

JustMe234

Joined Feb 25, 2017
68
Hello,
I am trying to make an software to control pc volume from Arduino with C#. I have made almost all the soft but i have stuck at Volume Bar.
The buttons works just fine.
I want to control the volume with Potentiometer connected to Arduino and send signal to the soft on pc.

I think to connect with TrackBar or ScrollBar. I have made the code to control the volume on soft but can't make it to work with Arduino.

Thank you

The trackbar and Count are declared up

C:
public void trackBar1_Scroll(object sender, EventArgs e)
        {
            label3.Text = trackBar1.Value.ToString();
            if (Count == 1)
            {
                if (trackBar1.Value > trackBar)
                {
                    keybd_event(VK_VOLUME_UP, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
                }
                if (trackBar1.Value < trackBar)
                {
                    keybd_event(VK_VOLUME_DOWN, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
                }

            }
           trackBar = trackBar1.Value;
            Count = 1;
      
        }
 
Last edited by a moderator:

MrSoftware

Joined Oct 29, 2013
2,202
If you can control volume from code then great, that's the hard part. Next google how to receive serial data from a COM port in C#.

Now on the arduino, when you want to turn the volume up on the pc, do something like Serial.println("UP"). When you run your code using the arduino ide, that string traveled from the arduino to your PC through a virtual COM port. You want to catch that string using C#. So google how to read strings from a COM port using C#. Now in your C# code, read from the arduino's serial (COM) port and every time you receive "UP", execute your code to turn up the volume. It's just that easy!
 

ErnieM

Joined Apr 24, 2011
8,377
Why do you even need the trackbar? The Arduino sends the volume information, the C# program gets this and changes the volume with code. Nothing need be on the screen for this to happen. If you wish for it, why not use the already built in Windows volume control to show the level?
 

Thread Starter

JustMe234

Joined Feb 25, 2017
68
If you can control volume from code then great, that's the hard part. Next google how to receive serial data from a COM port in C#.

Now on the arduino, when you want to turn the volume up on the pc, do something like Serial.println("UP"). When you run your code using the arduino ide, that string traveled from the arduino to your PC through a virtual COM port. You want to catch that string using C#. So google how to read strings from a COM port using C#. Now in your C# code, read from the arduino's serial (COM) port and every time you receive "UP", execute your code to turn up the volume. It's just that easy!
Thanks for reply
I have set the communication between Arduino and Pc with serial communication and works great the buttons work.

Now, i want to use a Potentionmeter to send values to pc and the pot send value from 0-1023. I understood the idea but when the pot is turn for e.x 0-500 and send to pc "up" value the volume it will turn all to 100%.
 

Thread Starter

JustMe234

Joined Feb 25, 2017
68
Why do you even need the trackbar? The Arduino sends the volume information, the C# program gets this and changes the volume with code. Nothing need be on the screen for this to happen. If you wish for it, why not use the already built in Windows volume control to show the level?

I thought to control trackbar value from Arduino so the volume it will turn up and down.
Example if the trackbar has value from 0-100 i thought to send value from Arduino 0-100 so the trackbar will move, but it won't this doesn't work.

This is the command for turn volume up
keybd_event(VK_VOLUME_UP, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);

I don't know other way to do it.
 

MrSoftware

Joined Oct 29, 2013
2,202
Connect the pot to an analog in put on the arduino, and put a pull-down resistor on the same arduino pin, and connect the pot input to V+. Double check the arduino spec, but I think you do not want the voltage input to the analog arduino pin to go higher than the voltage the arduino is running at (3.3v or 5v). The arduino will have a 5v pin that you can use to power the pot. Set the pin as analog input and use the arduino analogRead() function to read the value on the pin, and send that value up to the PC.
 

Thread Starter

JustMe234

Joined Feb 25, 2017
68
I have done that now the problem is how to stop send value when the pot is not moving. And when moving backwards send different value.
 

Thread Starter

JustMe234

Joined Feb 25, 2017
68
I made it Thanks all
Take a look


C:
int Up = 0;
int Down = 0;

void setup() {

Serial.begin(9600);
}

void loop() {

int sensorValue = analogRead(A0);
  sensorValue = map (sensorValue, 0, 1023, 0, 100);

  if (sensorValue > Up){
    Serial.print("1");
    Up = sensorValue;
    Down = sensorValue;
  }

  if (sensorValue < Down){
    Serial.print("2");
    Down = sensorValue;
    Up = sensorValue;
  }
  delay(15); // Change to get slower value

}
 
Last edited by a moderator:

MrSoftware

Joined Oct 29, 2013
2,202
Good job getting it working! One more piece of advice; make the arduino side "Stupid". What I mean is, don't store any values or do any comparisons on the arduino. Only have the arduino read the value from the pot and relay that value up to the PC.

int iPinVal = analogRead(MY_PIN);
Serial.println(ipinVal);

On the PC read the string, convert it to an int then use that to set the volume. Volume level is an attribute of the PC, the arduino doesn't need to know about it, and trying to maintain volume data on the arduino and the PC has the potential to cause complications.

Actually the better solution would be to get rid of the POT and instead use a rotary encoder, and with the arduino only detect the direction that the encoder is turning. If the user turns it to the right, send "UP" to the PC, and if the user turns it to the left send "DOWN" to the PC. Now the arduino is truly not tracking any state at all, and only sending raw user intput to the PC, which will be the most problem free way to do it. Buttons on the arduino would also work.
 

Thread Starter

JustMe234

Joined Feb 25, 2017
68
Thanks for your advice. In future i will try to make it more effective.

I like the feeling of turn a pot that's why i use Pot.
 
Top