LED dimming using analogeWrite and MIT app inventor

Hi This is my first time using Arduino for a school project. I am working on dimming a LED panel using PWM pins on Arduino Uno and I have a problem in my code. I was able to dim it using analogWrite(light, 127) automatically. However what I want is to dim it using an App that was created using MIT app inventor.

The app has 4 buttons (100%, 75%, 50%, and 25%) for each I want to use (analogWrite(light, 255), analogWrite(light, 191), analogWrite(light, 127), analogWrite(light, 64)) respectively. When I used an if statement the analogWrite didn't function and when I tried digitalWrite it worked but its only setting it high or low. The app is sending a text "1" or "2" "3" and so on to the bluetooth module HC-06.

Is there a way I can use analogWrite for these four stages? and can you please explain how or walk through the steps.

Thank you in advance

I have attached the Arduino code it is very simple and the MIT app code

I don't see any if statements. What LED panel?

I am using a LED panel from China but Im testing it first on a small led.

This is the code:

int light = 9;

void setup() {

Serial.begin(9600);
pinMode(light, OUTPUT);

}

void loop() {

if(Serial.available() > 2)
{
int received = Serial.read();
Serial.println (received);

if (received == 1 )
{
analogWrite(light, 255);
}

else if (received == 2)
{
analogWrite(light, 191);
}

else if (received == 3)
{
analogWrite(light, 127);
}

else if (received == 4)
{
analogWrite(light, 64);
}
}

Is the printing showing you the signals you are expecting?

Why > 2

Do you know what ASCII is?

From the serial monitor I am getting (49,50,51,52) for buttons (100%, 75%, 50% and 25%)and nothing is working

JATT:
From the serial monitor I am getting (49,50,51,52) for buttons (100%, 75%, 50% and 25%)and nothing is working

Yeah, there's a secret cipher called ASCII where 49 = 1, 50 = 2, 51 = 3, and 52 = 4.

If you changed 'if received == 1' to == 49, it'd work.

Or you can throw in some ' marks around '1' and such. Chars are different from values.

not sure, is it supposed to be greater than 0?

Many thanks for your reply it is working now but there is a problem

When pressing 100% I get full brightness

Wehn I press 75% it takes 3 times to go to the next level of brightness
and same thing happens when I press 50% 3 times

From serial monitor:

52
52
49

49
49
50

50
50
51

I am wondering if this is a delay problem

if(Serial.available() > 2)
Change to:
if(Serial.available() > 0)

Got it thanks

I still have a problem when I press any button it takes 3 times to go to the next level of brightness any idea why its happening

From serial monitor:

52
52
49

49
49
50

50
50
51

Can you post your code?

Here is the code

int light = 9;

void setup() {

Serial.begin(9600);
pinMode(light, OUTPUT);

}

void loop() {

if(Serial.available() > 0)
{
int received = Serial.read();
Serial.println (received);

if (received == 49)
{
analogWrite(light, 64);
}

else if (received == 50)
{
analogWrite(light, 127);
}

else if (received == 51)
{
analogWrite(light, 191);
}

else if (received == 52)
{
analogWrite(light, 255);
}
}
}

I can't see any reason that you should have to enter a character more than once.
Are you sure that is the code you have uploaded?

  int received = Serial.read();
  Serial.println (received);
 
  if (received == '1')

etc makes things easier to read.

If its taking 3 presses you've still got the available > 2 version uploaded.

Check the compile/upload succeeds every time you compile/upload by looking at the warnings/error window at
the bottom of the sketch pane - uploads can and do fail, and if in doubt upload again.

Yea I had the > 2 version uploaded instead but now its working thank you guys for your help :slight_smile:

I am now working on adding an increase and decrease button to increase the brightness by 10% or 5% every time the button is being clicked.

Any idea on how to add that to the code or useful resources that I can look at cause I want it to increase/decrease depending on if someone previously clicked on the 25%-50%-75%-100% button.

I tried increasing it by adding almost 12.7 (10%) to the value and manually put that in the code but I want it with an if statement so that I can use the app hopefully thats clear

I would appreciate any idea

analogWrite(light, 256);
delay(2000);

analogWrite(light, 241);
delay(2000);

analogWrite(light, 229);
delay(2000);

analogWrite(light, 216);
delay(2000);

analogWrite(light, 203);
delay(2000);

analogWrite(light, 191);
delay(2000);

analogWrite(light, 178);
delay(2000);

analogWrite(light, 165);
delay(2000);

analogWrite(light, 152);
delay(2000);

analogWrite(light, 140);
delay(2000);

analogWrite(light, 127);
delay(2000);

analogWrite(light, 256);
delay(2000);

analogWrite only works with 0 to 255

256 might wrap around to 1.

I added the increase decrease however I noticed two issues:

One;

when I connected the oscilloscope to the circuit and pressed High button (75% duty cycle) I am getting a flipped signal same as when I press Low button (25% duty cycle). I used a npn transistor (PN2222) the base to 1K to Arduino pin 9, emitter to ground, and collector to Dim+ of panel and Dim- to ground of Arduino.

I am not sure why its flipped or if its supposed to be like that?

I attached a screenshot of the scope when pressing High button.

int panel1 = 9;
int x = 0;
int y = 0;

void setup() {
          
            Serial.begin(9600);
            pinMode(panel1, OUTPUT);
              
          }
          
          void loop() {
          
             if(Serial.available() > 0)
             {
              int received = Serial.read();
              Serial.println (received);
             
                  if (received == '1')          // if High is clicked 
                  {
                    analogWrite(panel1, 191);  // 75% duty cycle 
                    x = 191;
                    y = 191;
                  }
                
                  else if (received == '2')     // if Med is clicked 
                  {
                    analogWrite(panel1, 127);  // 50% duty cycle
                    x = 127;
                    y = 127;
                  }
                
                  else if (received == '3')     // if Low is clicked
                  {
                    analogWrite(panel1, 64);   // 25% duty cycle 
                    x = 64;
                    y = 64;
                  }
                
                  if (received == '4')         // if increase is clicked 
                  {
                    int newx = x - 13;
                    analogWrite(panel1, newx);
                    x = newx;
                    if (newx <= 217)
                    {
                      x = 271;
                    }
                    }
                  }
                  if (received == '5')
                  {
                    int newy = y + 13;
                    analogWrite(panel1, newy);
                    y = newy;
                    if (newy >= 38)
                    {
                      y = 38
                    }
                  }
                 }
}

If you look at pin 9 you will see the inverse what’s on the collector of the 2N2222.

I didn't really get what you meant

the probe of the scope is connected to the base of npn that is connected to pin 9 and the ground are you saying that I should connect it to the collector instead