Code not working potentiometer in r4 minima

Hi my name Luigi from Italy my English elementary from study hobby.

My problem code not working in r4 minima with out WIFI.

my code:


const int RED = 3;
const int GREEN = 6;
const int BLUE = 5;

void color_clear();
void set_color(int R, int G, int B);

int potValue;
int potMaxValue = 3;
int potMap;
int PIN_POT =  14;

void setup() {
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  Serial.begin(115200);
  color_clear();
}

void loop() {
  potValue = analogRead(PIN_POT);
  potMap = map(potValue, 0, 1023, 0, potMaxValue);


  if (potMap == 1)
  {
    Serial.println("RED");
    set_color(255,0,0);
  }
  else if (potMap == 2)
  {
    Serial.println("GREEN");
    set_color(0,255,0);
  }
  else if(potMap == 3)
  {
    Serial.println("BLUE");
    set_color(0,0,255);
  }else
  {
    Serial.println("OFF");
    color_clear();
  }
  Serial.println(potMap);
  delay(30);
}


void set_color(int R, int G, int B){
  analogWrite(RED, R);
  analogWrite(GREEN, G);
  analogWrite(BLUE, B);
}

void color_clear(){
  digitalWrite(RED, LOW);
  digitalWrite(GREEN, LOW);
  digitalWrite(BLUE, LOW);
}

Not working from r4 minima.

Upload sketch nano r3 .. code normal running.

My pinout pontetiometer:
image

Some more debug prints?
A0 is more architecture neutral than 14.

But by chance I have to put 4096 in the adc max.

Tomorrow I try now good night for me in Italy.

Hi, I checked the simple simple circuit well.. but the code doesn't work correctly.. I attach photos and new code.. I put 4096 because outside of the arduino uno boards. The esp32 boards have the potentiometer which is 4096 adc max. But I put it like this because I had a doubt that here too.. it should be put like this.. but it's not like that. Idea ? Good day.

image


const int RED = 3;
const int GREEN = 6;
const int BLUE = 5;

void color_clear();
void set_color(int R, int G, int B);

int potValue;
int potMaxValue = 3;
int potMap;

void setup() {
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  Serial.begin(115200);
  color_clear();
}

void loop() {
  potValue = analogRead(A0);
  potMap = map(potValue, 0, 4096, 0, potMaxValue);


  if (potMap == 1)
  {
    Serial.println("RED");
    set_color(255,0,0);
  }
  else if (potMap == 2)
  {
    Serial.println("GREEN");
    set_color(0,255,0);
  }
  else if(potMap == 3)
  {
    Serial.println("BLUE");
    set_color(0,0,255);
  }else if (potMap == 0)
  {
    Serial.println("OFF");
    color_clear();
  }
  Serial.println(potMap);
  delay(30);
}


void set_color(int R, int G, int B){
  analogWrite(RED, R);
  analogWrite(GREEN, G);
  analogWrite(BLUE, B);
}

void color_clear(){
  digitalWrite(RED, LOW);
  digitalWrite(GREEN, LOW);
  digitalWrite(BLUE, LOW);
}

Why bother?
Why not just

set_color (0,0,0) ;

Maybe there are.. I would like to know how I can do that:

1: Red
2: Green
3: Blue

The problem is the arduino-cli cache, which always loaded the old one for me.. I emptied the temporary folder of the system and now it seems to work as I want.. In addition, I changed the potentiometer.. I am attaching semi-functional code. To make that I would like to do as I put you in the above list what should I do?.
In this piece of code:
potMap = map(potValue, 0, 1023, potMinValue, potMaxValue);

that I have to change so that the 4 does not come in the serial monitor when I turn the potentiometer ?.

Thank you very much and have a nice day.


#define RED 3
#define GREEN 6
#define BLUE 5
int POT = 14;

void set_color(int R, int G, int B);

int potValue;
int potMinValue = 1;
int potMaxValue = 4;
int potMap;
const char * color;
void setup() {
  Serial.begin(115200);
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  set_color(0,0,0);
}

void loop() {
  potValue = analogRead(POT);
  potMap = map(potValue, 0, 1023, potMinValue, potMaxValue);

  if (potMap == 1)
  {
    color = "RED";
    set_color(255,0,0);
  }
  else if (potMap == 2)
  {
    color = "GREEN";
    set_color(0,255,0);
  }
  else if(potMap == 3)
  {
    color = "BLUE";
    set_color(0,0,255);
  }else
  {
    color = "OFF";
    set_color(0,0,0);
  }
  // Serial.println(color);
  Serial.println(potMap);
  delay(30);
}

void set_color(int R, int G, int B){
  analogWrite(RED, R);
  analogWrite(GREEN, G);
  analogWrite(BLUE, B);
}

ok, fixed code:

Because if I just touch the potentiometer.. 4 comes out. ? I would like 1 to 3. And sometimes a 4 comes up every once in a while.


#define RED 3
#define GREEN 6
#define BLUE 5
int POT = 14;
void set_color(int R, int G, int B);
int potMinValue = 1;
int potMaxValue = 4;
int potValue,potPreviousValue;
const char * color;
void setup() {
  Serial.begin(115200);
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  set_color(0,0,0);
}

void loop() {
  potValue = analogRead(POT);
  potValue = map(potValue, 0, 1023, potMinValue, potMaxValue);
  if (potValue != potPreviousValue)
  {
    if (potValue == 1)
    {
      color = "RED";
      set_color(255,0,0);
    }
    else if (potValue == 2)
    {
      color = "GREEN";
      set_color(0,255,0);
    }
    else if(potValue == 3)
    {
      color = "BLUE";
      set_color(0,0,255);
    }else
    {
      color = "OFF";
      set_color(0,0,0);
    }
    potPreviousValue = potValue;
    Serial.println(color);
    Serial.println(potValue);

  }
  delay(500);
}
void set_color(int R, int G, int B){
  analogWrite(RED, R);
  analogWrite(GREEN, G);
  analogWrite(BLUE, B);
}

I think you need to use the constrain function before you apply the map function.