Converting serial input

Hi,

I have a question about converting a serial reading into a value (e.g. 250).
I've bought the Adafruit 16-channel PWM and want to controll the brightness of a GRB-led (for learning how the board and library are working).
So I thought it would be nice to controll it via the commandline of the serial display of the arduino IDE.
Unfortunately it doesn't work with the code I'm using (a modified version of the adafruit examplesketch). My idea was that I have to converte the serial reading, but don't know how to do that.
I would be happy if someone had an idea what to do.

Below the code I'm running on my arduino.

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();// called this way, it uses the default address 0x40

int value_1=0;//value_1 has the value 0

void setup() {
 Serial.begin(9600);
  pwm.begin();//initialise the board
  pwm.setPWMFreq(1000);  // set the pulse frequency
  uint8_t twbrbackup = TWBR;  // save I2C bitrate
  TWBR = 12; // upgrade to 400KHz (I2C communication speed)!
    
}

void loop() {
  
  if (Serial.available())    // when characters arrive over the serial port...
  {
    delay(100);    // wait a bit for the entire message to arrive
    while (Serial.available() > 0)     // read the available value
    {
            value_1=Serial.read();//value_1 stores the value from 'Serial.read'
    }
   }
      
    
       
      pwm.setPWM(0,0,value_1);//blue (scematic: pwm.setPWM(output,minimum PWM value, max.PWM value))
      pwm.setPWM(1,0,value_1);//green
      pwm.setPWM(2,0,value_1);//blue
      pwm.setPWM(3,0,value_1);//red
}

Thanks for help;)

You must convert the value received in ascii to a integer value.

Tank you. I did that but it generates values greater 4000 when sending e.g. 200 (I monitor it over the serial display as seen below).

[...]
 value_1=int(Serial.read());//'Serial.read' is converted to an 'int'
 Serial.print(value_1);//monitor the int -value
[...]

2future.

 value_1=int(Serial.read());//'Serial.read' is converted to an 'int'

Serial.read() returns an int. There is absolutely no need to cast an int to an int.

The int that it returns is so that it can return an error condition if there is nothing to read. The real data, if there was something to read, is in the low order byte of the int. Depending on what sent the data, that is either a character or a binary value.

If the Serial Monitor application is being used to send the data, you get characters. You need to collect the characters into an array, keeping it NULL terminated, and then use atoi() to convert the array to an int.

Or, be lazy and use Serial.parseInt().

By the way, the comment is wrong. The value is not converted to an int. It is cast as an int. BIG difference.