Sending values only Once for each button push?

I want to print the RGB values to serial monitor only once when i push a button. When i push the button the values prints all the time and do not stop. How can i do that?

Here is my code:

int RedLED = 3;  // the PWM pin the LED is attached to
int GreenLED = 5;
int BlueLED = 6;
int button = 1;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);

  // declare LED pin to be an output:
  pinMode(RedLED, OUTPUT);
  pinMode(GreenLED, OUTPUT);
  pinMode(BlueLED, OUTPUT);
  pinMode(button, INPUT_PULLUP);
}

// the loop routine runs over and over again forever:
void loop() {
  int buttonValue = digitalRead(button);
  
  // reads the input on analog pin A0 (value between 0 and 1023)
  int R = analogRead(A0);
  int G = analogRead(A1);
  int B = analogRead(A2);

  // scales it to brightness (value between 0 and 255)
  int brightness1 = map(R, 0, 1023, 0, 255);
  int brightness2 = map(G, 0, 1023, 0, 255);
  int brightness3 = map(B, 0, 1023, 0, 255);

  // sets the brightness LED that connects to  pin 3
  analogWrite(RedLED, brightness1);
  analogWrite(GreenLED, brightness2);
  analogWrite(BlueLED, brightness3);

  
  if (buttonValue == 0){
  Serial.print("R: ");
  Serial.println(brightness1);
  Serial.print("G: ");
  Serial.println(brightness2);
  Serial.print("B: ");
  Serial.println(brightness3);

  }
  else{
    
  }
}

Take a look at the state change example in the IDE

1 Like

Hello
Your sketch needs an addtional variable to interlock the push button function.

millis() can be often a solution for such simple stuff

static uint32_t pushTime;
if (buttonValue == 0 && millis() - pushTime > 500){
  pushTime = millis(); 
  Serial.print("R: ");
  Serial.println(brightness1);
  Serial.print("G: ");
  Serial.println(brightness2);
  Serial.print("B: ");
  Serial.println(brightness3);
}

Got it :slight_smile:

Her is the code that works:

int RedLED = 3; 
int GreenLED = 5;
int BlueLED = 6;
int button = 1;

int buttonState = 0;
int lastButtonState = 0;

void setup() {
  Serial.begin(9600);

  pinMode(RedLED, OUTPUT);
  pinMode(GreenLED, OUTPUT);
  pinMode(BlueLED, OUTPUT);
  pinMode(button, INPUT_PULLUP);
}

void loop() {
  buttonState = digitalRead(button);

  int R = analogRead(A0);
  int G = analogRead(A1);
  int B = analogRead(A2);

  int brightness1 = map(R, 0, 1023, 0, 255);
  int brightness2 = map(G, 0, 1023, 0, 255);
  int brightness3 = map(B, 0, 1023, 0, 255);

  analogWrite(RedLED, brightness1);
  analogWrite(GreenLED, brightness2);
  analogWrite(BlueLED, brightness3);


  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      Serial.print("R: ");
      Serial.println(brightness1);
      Serial.print("G: ");
      Serial.println(brightness2);
      Serial.print("B: ");
      Serial.println(brightness3);

    }
    else {

    }
    delay(100);
    lastButtonState = buttonState;
  }
}
1 Like

One more question regards to this project after i added an 1602 LCD displat for displaying the values on.

When i push the button and sends the values to the LCD i get "254" when i turn the potentiometer to the brightest value, and "054" when i turn the potentiometers off.

Serial monitor shows also "254" as max value, but shows "0" when the led is off.

Here is my last code:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

int RedLED = 3;
int GreenLED = 5;
int BlueLED = 6;
int button = 1;

int buttonState = 0;
int lastButtonState = 0;

void setup() {
  lcd.init();
  Serial.begin(9600);
  pinMode(RedLED, OUTPUT);
  pinMode(GreenLED, OUTPUT);
  pinMode(BlueLED, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  lcd.backlight();

}

void loop() {
  buttonState = digitalRead(button);

  int R = analogRead(A0);
  int G = analogRead(A1);
  int B = analogRead(A2);

  int brightness1 = map(R, 0, 1023, 0, 255);
  int brightness2 = map(G, 0, 1023, 0, 255);
  int brightness3 = map(B, 0, 1023, 0, 255);

  analogWrite(RedLED, brightness1);
  analogWrite(GreenLED, brightness2);
  analogWrite(BlueLED, brightness3);


  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      Serial.print("R:");
      Serial.println(brightness1);
      Serial.print("G: ");
      Serial.println(brightness2);
      Serial.print("B: ");
      Serial.println(brightness3);

      lcd.setCursor(5, 0);
      lcd.print("R:");
      lcd.setCursor(7, 0);
      lcd.print(brightness1);

      lcd.setCursor(0, 1);
      lcd.print("G:");
      lcd.setCursor(2, 1);
      lcd.print(brightness2);

      lcd.setCursor(11, 1);
      lcd.print("B:");
      lcd.setCursor(13, 1);
      lcd.print(brightness3);

    }
    else {

    }
    delay(100);
    lastButtonState = buttonState;
  }
}

And here is two pictures:

You need some over-printing with spaces - think about how numbers are printed to the LCD; they're left-justified.

Okay, but why is it not show "255" as max value?

Replace that with
"R / 4;"

Or if you must use map, use map(R, 0, 1024, 0, 256);

Thanks! So simple :slight_smile:

Some progress update. From breadboard to perfboard.

The project is no finished and works perfectly. :slight_smile:

Here is a short video and some pictures, and off course the code.

And code:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 20, 4);

int RedLED = 3;
int GreenLED = 5;
int BlueLED = 6;
int button = 2;

int buttonState = 0;
int lastButtonState = 0;

void setup() {
  lcd.init();
  lcd.backlight();
  lcd.clear();
  Serial.begin(9600);
  pinMode(RedLED, OUTPUT);
  pinMode(GreenLED, OUTPUT);
  pinMode(BlueLED, OUTPUT);
  pinMode(button, INPUT_PULLUP);

  lcd.setCursor(1, 0);
  lcd.print("RGB VALUE CATCHER!");

  lcd.setCursor(0, 1);
  lcd.print("Red");

  lcd.setCursor(7, 1);
  lcd.print("Green");

  lcd.setCursor(16, 1);
  lcd.print("Blue");

  lcd.setCursor(0, 2);
  lcd.print("---    -----    ----");
}

void loop() {
  buttonState = digitalRead(button);

  int R = analogRead(A0);
  int G = analogRead(A1);
  int B = analogRead(A2);

  int brightness1 = map(R, 0, 1023, 0, 255);
  int brightness2 = map(G, 0, 1023, 0, 255);
  int brightness3 = map(B, 0, 1023, 0, 255);

  analogWrite(RedLED, brightness1);
  analogWrite(GreenLED, brightness2);
  analogWrite(BlueLED, brightness3);


  if (buttonState != lastButtonState) {
    if (buttonState == LOW) {
      Serial.print("R: ");
      Serial.println(brightness1);
      Serial.print("G: ");
      Serial.println(brightness2);
      Serial.print("B: ");
      Serial.println(brightness3);

      lcd.setCursor(0, 3);
      lcd.print("   ");
      lcd.setCursor(0, 3);
      lcd.print(brightness1);

      lcd.setCursor(8, 3);
      lcd.print("   ");
      lcd.setCursor(8, 3);
      lcd.print(brightness2);

      lcd.setCursor(17, 3);
      lcd.print("   ");
      lcd.setCursor(17, 3);
      lcd.print(brightness3);

    }
    else {

    }
    delay(50);
    lastButtonState = buttonState;
  }
}

Didn't like my suggestions, huh?

Yes, but when i get the circuit to the perfboard i must changed it back to «255».

I don't understand your response.

That was one of your suggestions.

I changed to that and that fixed the problem when i have my circuit on the breadboard.

But when i soldered my circuit to the perfboard i had to change it back to «255» because the led turned off when i give the potentiometer full value.

Nope, still not understanding your reply.

"R / 4" is simple and correct - it gives an even distribution of values.

"map ( R, 0, 1023, 0, 255)" is expensive and incorrect - it gives an uneven distribution of values.

"map (R, 0, 1024, 0, 256)" is expensive, but gives the same results as "R / 4"

I think i do not understand your reply either…

Here you go:

void setup() 
{
  Serial.begin (115200);

  int oopsCount = 0;
  for (int rawADC = 0; rawADC < 1024; rawADC++) {
    if (rawADC / 4 != map (rawADC, 0, 1023, 0, 255))
      oopsCount ++;
  }
  Serial.print (F("Expensive bad oopsCount = "));
  Serial.println (oopsCount);

  oopsCount = 0;
  for (int rawADC = 0; rawADC < 1024; rawADC++) {
    if (rawADC / 4 != map (rawADC, 0, 1024, 0, 256))
      oopsCount ++;
  }
  Serial.print (F("Expensive good oopsCount = "));
  Serial.println (oopsCount);
}

void loop() {}

Just more confused. Sorry.