PWM controller adafruit with Serial.available()

Hello there,

I`m working on a project where I want to control two different PWM pins using the PWM controller from adafruit using the Serial monitor to send data to the arduino.

Now, I managed to control one PWM pin, but I`m in doubt when it comes to Serial.available()
So my question is, is it possible to use Serial.available() more than once in the Loop ?

Here is loop part of code :

void loop()
{
  
  while(Serial.available() > 0)
  {
  int ch = Serial.parseInt();

  ch = constrain(ch,0,4095);

  brightness1k = ch;

  delay(5);
  }


  pwm.setPWM(11, 0, brightness1k);
  {
 delay(160);

 uint16_t lux = lightMeter.readLightLevel();

  delay(10); 

  if (lux<4306)
  {
      brightness1k++;
   
           
 }
  if (lux>4302)
  {
     brightness1k--;

  
  }
  }
  
}

Yes, no problem.

But all those delays will probably give you an headache if you want to do two things...

Thank you :) Now, how can i use more than one Serial.available without interrupting each other ?

And I want to control the pwm pins with any value i enter in the Serial bar/monitor.

Serial.available() is a quick and non blocking function. So you don't need to do anything. So if you have problems it's probably somewhere else ;) Presumably PEBCAK ;)

Did you read Robin2's excellent updated Serial Input Basics tutorial?

I`m afraid I haven not, but i got it working and now moved to the next step :) thank you.

[insert irony]Thanks for sharing that solution ;)

haha Here is my solution :

void loop()
{

while(Serial.available() > 0)
{
int ch = Serial.parseInt();

ch = constrain(ch,2170);

brightness1k = ch;

delay(5);
}

pwm.setPWM(11, 0, brightness1k);
{
delay(160);

uint16_t lux = lightMeter.readLightLevel();

delay(10);

if (Serial.available()==4)

pwm.setPWM(11, 0, brightness6k);

if (lux<4306)
{
brightness1k++;

}
if (lux>4302)
{
brightness1k–;

}
}

}

As it goes through the code from top to bottom, the second "if (Serial.available()==4) "
means that if the 4 characters have been entered in the Serial window, do the stuff right underneath it.

Serial Input Basics - simple reliable ways to receive data.

...R

Hi everyone,

I`m trying to use more than Serial.available in my code but it seems to only read the first Serial.available and ignore the rest. Anyone has an idea why it does that ?

void loop(){
 delay(160);
 uint16_t lux = lightMeter.readLightLevel();
  Serial.print("Light= ");
  Serial.println(lux);
  Serial.println("lux");
  
  if (Serial.available() > 0)
  {
  int ch1 = Serial.parseInt();
  ch1 = constrain(ch1,0,2180);
  brightness1k = ch1;
  delay(5);
  }
  pwm.setPWM(11, 0, brightness1k);
  Serial.println("  ");
  Serial.print("brightness1k=");
  Serial.println(brightness1k);
  
while (Serial.available() > 4)   
  {
  int ch6 = Serial.parseInt();
  ch6 = constrain(ch6,3900,4050);
  brightness6k=ch6 ;
  delay(5); 
  pwm.setPWM(7, 0, brightness6k);
  }

  {
  if (lux<4080) 
  {
      brightness1k++;
 }
 
  if (lux>4080)
  {
     brightness1k--; 
  }
  
  if (lux != 4200 && lux > 25600)
{
 brightness6k--; 
}

else if (lux != 4200 && lux < 25600){ 
 brightness6k++; 

}
     
 }
  
}

Hi,

What is your code supposed to do?

This may be helpful.

Serial Input Basics

Tom… :slight_smile:

Im suppose to control two pwm pins. By plotting a value in the Serial buffer, the pwm pin (11) works fine with the constrained values. Now the second part where the second (serial.available >4 ) it never recognize any value i put it after putting a value the first time.

It is suppose to read the first value entered and turn on Pin 11 and then when another/different value is entered, i need it to turn on Pin 7, and keep pin 11 on.

I`m using an adafruit pwm controller together with Arduino uno and Cree LEDs.

Just a guess as you do not say how you send your data. You also do not explain what you expect your code to do.

Let's say that you send 5 bytes as one bunch when your code is in the delay(160) (2 bytes for ch1 and 3 bytes for ch6. The first serial available indicates that data is available and reads all bytes needed for an int (5). Next your code encounters the second available() but there is nothing available so the while is ignored.

I guess you will be far better of sending two values separated by a comma; see e.g. the updated Serial Input Basics thread for ideas.

Thanks for the reply.

Does that mean when i type in a pwm value such as 2180, it get sent 4 bytes to the first Pwm pin which is 11 ??

I had a look at the link you have attached but did not get alot out of it.

Think about what this does.

while (Serial.available() > 4)

Even though i put it this way, it still gets ignored.

if (Serial.available() > 0) { int ch1 = Serial.parseInt(); ch1 = constrain(ch1,0,2180); brightness1k = ch1; delay(5); } pwm.setPWM(11, 0, brightness1k); Serial.println(" "); Serial.print("brightness1k="); Serial.println(brightness1k);

while (Serial.available() >0) { int ch6 = Serial.parseInt(); ch6 = constrain(ch6,3900,4050); brightness6k=ch6 ; delay(5); pwm.setPWM(7, 0, brightness6k); }

bakersalama:
Does that mean when i type in a pwm value such as 2180, it get sent 4 bytes to the first Pwm pin which is 11 ??

It will be stored in ch1

bakersalama:
I had a look at the link you have attached but did not get alot out of it.

Read it again; and again; and again; and …

You can send two numbers in the form of <21,4567>. Once you have received the full string (part between < and >, see example #3, receive with start- and end-marker), you can parse it to separate the two numbers and convert each to an integer.

bakersalama: Even though i put it this way, it still gets ignored.

You still haven't told us in which format you send the data. And if all characters are consumed by the first Serial.available(), chances are extremely slim that there is new data available by the time you get to the second one.

I send it in integers such as 2170 or 4020 representing pwm values for 12bit controller. Sorry if i can not explain everything clearly. But as far as i understand and see my problem is within the Serial.available ()

trying many stuff with the code got me really confused, so that is why i`m asking what is the right way to approach and use the Serial.available () when sending pwm values to any channel.

You have a synchronization issue between the transmitter and receiver.

bakersalama:
I send it in integers such as 2170 or 4020 representing pwm values for 12bit controller. Sorry if i can not explain everything clearly. But as far as i understand and see my problem is within the Serial.available ()

trying many stuff with the code got me really confused, so that is why i`m asking what is the right way to approach and use the Serial.available () when sending pwm values to any channel.

So you send 2170 for the first one and 5 seconds later 4020 for the second one; you do not know where your code is so the result is unknown. Or you send 21704020 and expect the Arduino to be able to determine (with some magic) if it’s 2170 and 4020 or 21704 and 020? Not going to work :wink:

Based on the thread linked you have a few choices; e.g.
1)
<n,x> where n identifies the channel and x gives the value.
2)
<x1,x2,x3,…,x16> where you send sixteen values to the Arduino (one for each channel).

boolrules mentions synchronisation; the < and > take care of that