Hello, noobie to Arduino and coding here thoroughly enjoying the learning process! Looking for some help fading leds. I am using the RemoteXY app to communicate to the arduino. On the app i have a simple 3 position select switch. I think what i want is really simple and basic i'm just finding overly complicated ways of doing it through google searches. Bassically what i want to achieve is:
A. Select_switch_position Led is off/fades to off
B. Select_switch_position Led fades to 50% brightness
C. Select_switch_position Led fades to 100% brightness
Here is where i got to with the code. It is clearly wrong otherwise i wouldn't be here, but at least you can hopefully see how i have tried to set it up.
/*
-- New project --
This source code of graphical user interface
has been generated automatically by RemoteXY editor.
To compile this code using RemoteXY library 2.4.3 or later version
download by link http://remotexy.com/en/library/
To connect using RemoteXY mobile app by link http://remotexy.com/en/download/
- for ANDROID 4.7.12 or later version;
- for iOS 1.4.7 or later version;
This source code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
*/
//////////////////////////////////////////////
// RemoteXY include library //
//////////////////////////////////////////////
// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__HARDSERIAL
#include <RemoteXY.h>
// RemoteXY connection settings
#define REMOTEXY_SERIAL Serial
#define REMOTEXY_SERIAL_SPEED 9600
// RemoteXY configurate
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
{ 255,1,0,0,0,11,0,11,168,1,
3,3,19,14,25,68,192,26 };
// this structure defines all the variables and events of your control interface
struct {
// input variables
uint8_t LIGHTS; // =0 if select position A, =1 if position B, =2 if position C, ...
// other variable
uint8_t connect_flag; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
int led = 9;
int brightness = 0; // how bright the LED is
int brightness2 = 100;
int brightness3 = 244;
int dt=20;
int fadeAmount = 2; // how many points to fade the LED by
/////////////////////////////////////////////
// END RemoteXY include //
/////////////////////////////////////////////
void setup()
{
RemoteXY_Init ();
pinMode(led, OUTPUT);
// TODO you setup code
}
void loop()
{
RemoteXY_Handler ();
if (RemoteXY.LIGHTS==0) {
analogWrite(led, brightness);
}
else if (RemoteXY.LIGHTS==1) {
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 120) {
fadeAmount = -fadeAmount;
}
else if (RemoteXY.LIGHTS==2) {
analogWrite(led, brightness3);
}
}
}
Yay, i got closer to what i want! Any advice on improving the code always appreciated!
/*
-- New project --
This source code of graphical user interface
has been generated automatically by RemoteXY editor.
To compile this code using RemoteXY library 2.4.3 or later version
download by link http://remotexy.com/en/library/
To connect using RemoteXY mobile app by link http://remotexy.com/en/download/
- for ANDROID 4.7.12 or later version;
- for iOS 1.4.7 or later version;
This source code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
*/
//////////////////////////////////////////////
// RemoteXY include library //
//////////////////////////////////////////////
// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__HARDSERIAL
#include <RemoteXY.h>
// RemoteXY connection settings
#define REMOTEXY_SERIAL Serial
#define REMOTEXY_SERIAL_SPEED 9600
// RemoteXY configurate
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
{ 255,1,0,0,0,11,0,11,168,1,
3,3,19,14,25,68,192,26 };
// this structure defines all the variables and events of your control interface
struct {
// input variables
uint8_t LIGHTS; // =0 if select position A, =1 if position B, =2 if position C, ...
// other variable
uint8_t connect_flag; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
int led = 9;
int brightness = 0; // how bright the LED is
int brightness2 = 100;
int brightness3 = 244;
int dt=20;
int fadeAmount = 2; // how many points to fade the LED by
/////////////////////////////////////////////
// END RemoteXY include //
/////////////////////////////////////////////
void setup()
{
RemoteXY_Init ();
pinMode(led, OUTPUT);
// TODO you setup code
}
void loop()
{
RemoteXY_Handler ();
if (RemoteXY.LIGHTS==0) {
analogWrite(led, brightness);
if(brightness > 0){
brightness = brightness - fadeAmount;
}
delay(dt);
}
else if (RemoteXY.LIGHTS==1) {
analogWrite(led, brightness);
if (brightness < 100){
brightness = brightness + fadeAmount;
}
delay(dt);
}
else if (brightness > 100){
brightness = brightness - fadeAmount;
}
delay(dt);
}
Ok let me try digest what you're suggesting there.
In the meantime.. this seems to have worked...
/*
-- New project --
This source code of graphical user interface
has been generated automatically by RemoteXY editor.
To compile this code using RemoteXY library 2.4.3 or later version
download by link http://remotexy.com/en/library/
To connect using RemoteXY mobile app by link http://remotexy.com/en/download/
- for ANDROID 4.7.12 or later version;
- for iOS 1.4.7 or later version;
This source code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
*/
//////////////////////////////////////////////
// RemoteXY include library //
//////////////////////////////////////////////
// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__HARDSERIAL
#include <RemoteXY.h>
// RemoteXY connection settings
#define REMOTEXY_SERIAL Serial
#define REMOTEXY_SERIAL_SPEED 9600
// RemoteXY configurate
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
{ 255,1,0,0,0,11,0,11,168,1,
3,3,19,14,25,68,192,26 };
// this structure defines all the variables and events of your control interface
struct {
// input variables
uint8_t LIGHTS; // =0 if select position A, =1 if position B, =2 if position C, ...
// other variable
uint8_t connect_flag; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
int led = 9;
int brightness = 0; // how bright the LED is
int brightness2 = 100;
int brightness3 = 244;
int dt=50;
int fadeAmount = 2; // how many points to fade the LED by
/////////////////////////////////////////////
// END RemoteXY include //
/////////////////////////////////////////////
void setup()
{
RemoteXY_Init ();
pinMode(led, OUTPUT);
// TODO you setup code
}
void loop() {
// put your main code here, to run repeatedly:
RemoteXY_Handler ();
switch (RemoteXY.LIGHTS) {
case 0:
analogWrite(led, brightness);
if(brightness > 0){
brightness = brightness - fadeAmount;
}
delay(dt);
break;
case 1:
analogWrite(led, brightness);
if (brightness < 100){
brightness = brightness + fadeAmount;
}
if (brightness > 100){
brightness = brightness - fadeAmount;
}
delay(dt);
break;
case 2:
analogWrite(led, brightness);
if (brightness < 244){
brightness = brightness + fadeAmount;
}
if (brightness > 244){
brightness = brightness - fadeAmount;
}
delay(dt);
break;
}
}
Keep in mind that the LED has a certain minimum voltage at which it starts to light. Therefore, you cannot change the value 0-100-255. Find the minimum and middle voltage for each LED color experimentally.
It might be something like 90-160-240.
Yes i understand those values are arbitrary, the real goal here is as you say find the value you want for each setting and then achieve a nice dim/fade effect between settings. This is the real goal, sorry if that was unclear.