Go Down

Topic: Control 2 RGB LEDs using PWM with 3 Potentiometers (Read 494 times) previous topic - next topic

Zann

Somebody please help me. Almost a week I'm trying to solve my project. I'm not really sure if my codes are correct due to the procedure.

Please help me.. And I highly appreciate it...

Procedure:

Connect pins A0, A1 and A2 of the Arduino to the potentiometers. Also, connect pins 9-11 of the Arduino to pins R1, G1 and B1 respectively.

Create a program that detects two states (0 and 1, 0 means <=50% and 1 means >50%) for each three potentiometers and outputs the results to two RGB LEDs. Print the states of the potentiometers to Serial monitor. Follow the input-output relation in the table below:


POT1   POT2   POT3   RGB1   RGB2
0   0   0   RED   BLUE
0   0   1   BLUE   GREEN
0   1   0   GREEN   RED
0   1   1   RED   GREEN
1   0   0   BLUE   RED
1   0   1   GREEN   BLUE
1   1   0   BLUE   BLUE
1   1   1   RED   RED


Here the Materials:

1 x Arduino Uno board and USB cable
2 x RGB LEDs
1 x 150 Ω resistor
2 x 100Ω resistors and
3 x10k Ω potentiometer
1 x breadboard



Here's my code:


/*
Analog-To-Digital Conversion
*/
int redPin = 9; //Pin for the red RGB
int greenPin = 10; //Pin for the green RGB
int bluePin = 11; //Pin for the blue RGB

int Pin_Potentiometer1 = A0; //potentiometer for the LED
int Pin_Potentiometer2 = A1;
int Pin_Potentiometer3 = A2;

int readValue_red;  //read value from the potentiometer which controls the LED
int readValue_green;
int readValue_blue;

int writeValue_red;  //variable to send to the LED
int writeValue_green;
int writeValue_blue;

void setup() {

pinMode(Pin_Potentiometer1, INPUT); //potentiometer for the LED as input
pinMode(Pin_Potentiometer2, INPUT);
pinMode(Pin_Potentiometer3, INPUT);

pinMode(redPin,OUTPUT); //set pin for the LED as output
pinMode(bluePin,OUTPUT);
pinMode(greenPin, OUTPUT);
}

void loop() {

readValue_red = analogRead(Pin_Potentiometer1);  //Read voltage from potentiometer to control the LED
readValue_green = analogRead(Pin_Potentiometer2);
readValue_blue = analogRead(Pin_Potentiometer3);

writeValue_red = (255./1023.)*readValue_red;
writeValue_green = (255./1023.)*readValue_green;
writeValue_blue = (255./1023.)*readValue_blue;

analogWrite(redPin,writeValue_red);  //write value to set the brightness of the LED
analogWrite(greenPin,writeValue_green);
analogWrite(bluePin,writeValue_blue);
}

Grumpy_Mike

#1
Feb 21, 2018, 09:33 am Last Edit: Feb 21, 2018, 09:37 am by Grumpy_Mike
Quote
1 x 150 Ω resistor
2 x 100Ω resistors an
If you have two RGB LEDs you need six resistors. How have you wired up the resistors and the LEDs? We need to see a diagram before we can help.

You have to get the hardware correct before looking at software.

You also need to say what your code actually does and what you hope it would do.

Code: [Select]
(255./1023.)*readValue_blue;
That won't work unless you make the read value a float.


Grumpy_Mike

#3
Feb 21, 2018, 09:42 am Last Edit: Feb 21, 2018, 09:43 am by Grumpy_Mike
You have two LEDs in parallel. That is not good as the current will not be shared  equally. You need a resistor for each anode.

Zann

Sir Grumpy_Mike,

I highly appreciated your quick response.

Anyway Sir, I will make it separately I will put the other RGB LED to PIN3/5/6 but what should be the code that I will make?



I make the CODEs like this but not working properly.


/*
Analog-To-Digital Conversion
*/
int redPin9 = 9; //Pin for the red RGB
int greenPin10 = 10; //Pin for the green RGB
int bluePin11 = 11; //Pin for the blue RGB

int redPin3 = 3; //Pin for the red RGB
int greenPin5 = 5; //Pin for the green RGB
int bluePin6 = 6; //Pin for the blue RGB

int Pin_Potentiometer1 = A0; //potentiometer for the LED
int Pin_Potentiometer2 = A1;
int Pin_Potentiometer3 = A2;

int readValue_red3;  //read value from the potentiometer which controls the LED
int readValue_green5;
int readValue_blue6;

int readValue_red9;  //read value from the potentiometer which controls the LED
int readValue_green10;
int readValue_blue11;

int writeValue_red3;  //variable to send to the LED
int writeValue_green5;
int writeValue_blue6;

int writeValue_red9;  //variable to send to the LED
int writeValue_green10;
int writeValue_blue11;

void setup() {
   
   pinMode(Pin_Potentiometer1, INPUT); //potentiometer for the LED as input
   pinMode(Pin_Potentiometer2, INPUT);
   pinMode(Pin_Potentiometer3, INPUT);
   
   pinMode(redPin3,OUTPUT); //set pin for the LED as output
   pinMode(greenPin5, OUTPUT);
   pinMode(bluePin6,OUTPUT);
   
   pinMode(redPin9,OUTPUT); //set pin for the LED as output
   pinMode(greenPin10, OUTPUT);
   pinMode(bluePin11,OUTPUT);
   
}

void loop() {
   
   readValue_red3 = analogRead(Pin_Potentiometer1);  //Read voltage from potentiometer to control the LED
   readValue_green5 = analogRead(Pin_Potentiometer2);
   readValue_blue6 = analogRead(Pin_Potentiometer3);
   
   writeValue_red3 = (255./1023.)*readValue_red3;
   writeValue_green5 = (255./1023.)*readValue_green5;
   writeValue_blue6 = (255./1023.)*readValue_blue6;
   
   analogWrite(redPin3,writeValue_red3);  //write value to set the brightness of the LED
   analogWrite(greenPin5,writeValue_green5);
   analogWrite(bluePin6,writeValue_blue6);
   
   readValue_red9 = analogRead(Pin_Potentiometer1);  //Read voltage from potentiometer to control the LED
   readValue_green10 = analogRead(Pin_Potentiometer2);
   readValue_blue11 = analogRead(Pin_Potentiometer3);
   
   writeValue_red9 = (127./1023.)*readValue_red9;
   writeValue_green10 = (127./1023.)*readValue_green10;
   writeValue_blue11 = (127./1023.)*readValue_blue11;
   
   analogWrite(redPin9,writeValue_red9);  //write value to set the brightness of the LED
   analogWrite(greenPin10,writeValue_green10);
   analogWrite(bluePin11,writeValue_blue11);
   
}

Grumpy_Mike

#5
Feb 21, 2018, 12:55 pm Last Edit: Feb 21, 2018, 12:56 pm by Grumpy_Mike
Read what I said in reply #1.
You need to have float types of variables for the values you read and calculate. You currently have int - that is integer values which go to zero in your intermediate calculations.

Zann

Sir Grumpy_Mike,


Honestly, I don't know how to fix it, I don't know how to add the (float) on it. I spent a lot of time to fix this. I'm begging to complete my codes.

Grumpy_Mike

Where you declare the variables you use int
To fix your problem use float instead

Zann

Before anything else, How can you make it like this ==> Code: [Select] to minimize the code?

Yes, I did it. I changed the code from old into new one

Old CODEs

int writeValue_red3;  //variable to send to the LED
int writeValue_green5;
int writeValue_blue6;

int writeValue_red9;  //variable to send to the LED
int writeValue_green10;
int writeValue_blue11;


New CODEs

float writeValue_red3;  //variable to send to the LED
float writeValue_green5;
float writeValue_blue6;

float writeValue_red9;  //variable to send to the LED
float writeValue_green10;
float writeValue_blue11;



But the next problem is how to solve this issue Sir?

Create a program that detects two states (0 and 1, 0 means <=50% and 1 means >50%) for each three potentiometers and outputs the results to two RGB LEDs. Print the states of the potentiometers to Serial monitor. Follow the input-output relation in the table below:


POT1   POT2   POT3   RGB1   RGB2
0   0   0   RED   BLUE
0   0   1   BLUE   GREEN
0   1   0   GREEN   RED
0   1   1   RED   GREEN
1   0   0   BLUE   RED
1   0   1   GREEN   BLUE
1   1   0   BLUE   BLUE
1   1   1   RED   RED

Grumpy_Mike

#9
Feb 21, 2018, 05:31 pm Last Edit: Feb 21, 2018, 05:33 pm by Grumpy_Mike
Quote
Create a program that detects two states (0 and 1, 0 means <=50% and 1 means >50%) for each three potentiometers and outputs the results to two RGB LEDs.
This is sounding like homework, is this an assignment?

It seems very silly to use a pot to just act like a switch, your original code does much more than that.

If you have to do this then use a sequence of if statements like
Code: [Select]

// this is the code for the line ----> 101 GREEN   RED
if(readValue_red9 > 512 && readValue_green10 <= 512 && readValue_blue > 512){
digitalWrite(redPin9,HIGH);
digitalWrite(greenPin10,HIGH);
digitalWrite(bluePin11,LOW);
}

You have a hell of a lot of code in there that is of no use to you.

Go Up