Select button Led Brightness Fade

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

RemoteXY example:

https://remotexy.com/en/help/controls/select/

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);
  
}   

analogWrite takes a number up to 255. Restricting it to 100 isn't going to get you full brightness on your LED.

@jbogle3, your topic has been moved to a more suitable location on the forum.

Thanks for reply, this was for my middle setting. I'm actually having trouble coding a following else if statement to select 100% setting ie 244...

sorry.. *255

What are the possible values you can get from RemoteXY.LIGHTS?

That is a screenshot of the app. So you can select A,B or C

A is (RemoteXY.LIGHTS==0)
B==(RemoteXY.LIGHTS==1)
C==(RemoteXY.LIGHTS==2)

of course you could potentially keep going and add more select buttons but this is where im at.

I suggest that instead of your else if relating to brightness, you have an explicit test for RemoteXY.LIGHTS==2.

Ok... would you be able to elaborate a bit? Sorry, very new to this..

Just have a standalone if statement for each of the three values you can get from RemoteXY.LIGHTS.

It's a bit wasteful to do away with your else if constructs, but your Arduino has nothing else to do with its time and it'll be easier to understand.

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;
   
  }
}

Forget what I said - the switch approach you have is better.

Cool... Taken from the RemoteXY website. Its a great resource i'm finding!

Now what i'm finding is it's taking too long to get from C to A.. I'm thinking add a (dt/2) if brightness > 243 maybe or something like that..

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.