I am looking to fade through the LED colours using a trackbar in Visual Studio.
So far I have various buttons to turn the LED's on in different colours and others for PWM effects. However, I cannot get the trackbar to fade through the RGB colours.
I am connecting to an Arduino Mega 2560 via the serial port. I am new to this and to programming so my Arduino code is:
if (incomingByte == 'a')
{
//code to fade through the colours
}
I cannot get the trackbar to fade through the RGB colours.
Do you want the fade to happen automatically on receipt of an 'a' or do you want the fade to move to the next step each time an 'a' is received ?
I am not familiar with trackbars in VS but it is possible that you want the RGB colour on the Arduino to change depending on the position of a slider in VS. If that is the case then you would need to send a value corresponding to the position when it is changed rather than the same character each time.
Can you please expand on your requirements ?
What have you tried ? There must be dozens of examples on the Web and in this forum.
thats correct I am wanting the LED colours to change depending on the position of the slider.
Sending the same character each time the slider moves is not going to work then. Why not just send a number that represents the position of the slider and use the Arduino to convert this number to an RGB combination ? How many RGB steps do you want to display ? This will determine the range of the number to be sent to the Arduino.
So if I am correct this is sending 7 bytes for 7 steps on the slider.
If you are right then why send 7 bytes ? All you need to do is to send one value between zero and a maximum that represents the position of the slider.
trackBar1.Minimum = 0;
trackBar1.Maximum = 7;
Looks like the value will be between zero and 7, ie one of eight values.
As a start just read the serial input to the Arduino and print it on the serial monitor and post the program here so that we can see what you are doing.
Do you have means other than the Arduino to test the output from the slider in VS ?
You have changed the subject slightly by wanting to accept a string rather than a value from the slider but this should get you started.
You need to terminate the incoming string in some way so that the Arduino can recognise that a complete command has been received and act on it. You could use fixed length messages but using serial there is no guarantee that every character will be received as sent, so a delimited string is a safer way to do it.
I need to be able to read a byte then start fading. Read second byte from slider and start fading again and keep doing this until the slider is all the way to ten.
I don't get this. Please can you describe the actions that will be taken on the PC and the result you want to achieve on the Arduino as a result of the actions ?
Is this sending ten bytes i.e: A byte for each tick on the slider?
I don't know, but I still think that all you need to send is TrackBar1.Value.. Then on the Arduino read the incoming value when Serial data is available and call a function to set the RGB values appropriately for the value received, assuming that the slider position is to control the RGB output. However, once you describe what you want to happen more advice can be given.
Is this sending the textbox values (0-255) to the arduino, the Rx light is certainly flashing.
I don't know enough about VS to give advice on this. To start with how about just sending a byte with a fixed value to the serial port from VS rather than the value from the slider ? That way you know what you are sending and, therefore, what you expect to receive.
Is this the correct way of doing this, reading as an integer? Or should it be as Byte, Char etc.
Serial data arrives one byte at a time so that is what you need to read.
if(sliderVal >= '0' && sliderVal < '11')
This will not work because you are receiving bytes but testing for chars.
Write a program to wait until there is serial data available then read a byte and print it. If you follow my suggestion to send a known value then you will know if what you are getting is correct. Next step, send a series of known values, receive them and print them. Now you know that what you are sending is what you are receiving. Next step, send the slider value, receive it and only print it when it changes (or only send it when it changes).
Come back and post the Arduino code when you have got that working.
In the meantime can you describe what the relationship should be between the slider value received and the RGB value or are there going to be 3 sliders in VS, one each for R, G and B ?
I am just trying to get this first slider to work and know that it is working correctly then I was thinking about adding another two sliders for the RGB.
If that is the case then I would strongly advise that you proceed each of the individual RGB values with an identifier so that on the Arduino you can spilt the serial input and adjust the appropriate R, G or B value. So, the Arduino logic becomes
if serial input is available
read a byte
if the byte is R, G or B
read the next byte when it is available
call a function to adjust R, G or B output passing the colour identifier and byte value as parameters
end if
end if
Using that logic will allow you to send the R, G or B value only when it changes and for the Arduino to react to the changes. That way the serial traffic is much reduced compared with constantly sending the 3 values and the colour identifier allows the Arduino to get back in step if some serial data is missed or corrupted.
In your test code
*int red = Serial.parseInt();
int green = Serial.parseInt();
int blue = Serial.parseInt();
why is the red value a pointer and the green and blue ones not ?