Serial.read problem

Hi guys i am new here, trying to get arduino project working. And the thing is here is a arduino code

int pwm_a = 9;
int pwm_b = 10;
int pwm_c = 11;
int s=0;


void setup()
{

   pinMode(pwm_a, OUTPUT);
   pinMode(pwm_b, OUTPUT);
   pinMode(pwm_c, OUTPUT); 
      


   Serial.begin(9600);
}

void loop()

{
  
  if (Serial.available() > 0) {
  
        s=Serial.read();
        delay(50);
      Serial.write(s);


     analogWrite(pwm_a, s);
     analogWrite(pwm_b, s);
      analogWrite(pwm_c, s);
   
}
   
  }

And the thing is when i am trying to write a value from 0-255 (using serial monitor) Serial.write(s); returns right value but analogWrite(pwm_b, s); doesnt get the right value. (according to multimeter).
Can somebody help me with this? Thanks.

Sorry for some reason instead of Software ? Troubleshooting it endede up in Hardware ? Interfacing :-[

What sort of values are you sending, and why do you think that they're not being written correctly to the PWM?

Say you want a PWM value of 100 (giving an output duty cycle of about 40%) - you have to send a character value of 0x64, which is an ASCII 'd'.

BTW, don't try to make any inferences about the PWM output with a multimeter - you need an oscilloscope.

Thank you for fast reply
Well i was trying to send values like 50, 100 , 255 trough Serial monitor.

why do you think that they're not being written correctly to the PWM

When i am sending 255 trough Serial monitor Serial. write(s);
returns 255 and i get 2.7 volt on my PWM driver but when i enter the value in program (s=255;) The voltage is 10.9 volts.

BTW, don't try to make any inferences about the PWM output with a millimeter - you need an oscilloscope.

I am new at this so i might say something wrong. ;D

OK, I think you don't understand serial read.
It reads single bytes, so when you type "255" on the serial monitor, you're actually transmitting three ASCII values 0x32 0x35 0x35.
Your program sees these as separate bytes, and each time through loop (), sets the PWM values to 0x32, then 0x35 and 0x35 again.
The PWM registers never get set to 255.

The bit about not trying to read the voltage with a multimeter is because the PWM frequncy is about 500Hz - most multimeters won't read anything like as fast as this. Remember, PWM is not an analogue voltage, it is simply a constant frequency square wave with variable duty cycle.

There's a tutorial in the reference section

(sorry I took so long, I've just been out on a fire drill >:( )

Hm i don’t think it was that long:)
Ok now i understand what the problem
now the question is how to solve it… i tied

/* Led Pixel script - www.anthonymattox.com
 * recieves input from processing script  */

int led0=9; int led1=10; int led2=11;
int rval=0; int gval=0; int bval=0;

char buff[]= "0000000000";

void setup() {
  Serial.begin(9600);
  pinMode(led0,OUTPUT);
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
}

void loop() {
  analogWrite(led0,bval);
  analogWrite(led1,gval);
  analogWrite(led2,rval);

  while (Serial.available()>0) {
    for (int i=0; i<10; i++) {
      buff[i]=buff[i+1];
    }
    buff[10]=Serial.read();
    if (buff[10]=='R') {
      rval=int(buff[9]);
    }
    if (buff[10]=='G') {
      gval=int(buff[9]);
    }
    if (buff[10]=='B') {
      bval=int(buff[9]);
    }

  }
  delay(10);

}

And it actually did exactly same thing as mine :frowning:

Ooo, lotsa horrid stuff going on.
Very dodgy.

But it still only uses a single byte to set the PWM duty cycle.

You need to buffer three digits in a four byte buffer, terminate with a zero in the fourth byte, and use "atoi" to convert the string you've just buffered into an integer, then write that integer to your PWM.

Can you provide example or pseudo code of how to do it? i can't find tutorial anywhere.
Please:)

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1176289764
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1231650517

Ok thank you for information, i will try it later. And let you know.

Hmm still can't get it to work... The strange thing is in PROCCESSING this code works with my original aduino firmware

// Dimmer - sends bytes over a serial port
// by David A. Mellis

import processing.serial.*;
Serial port;

void setup() {
  size(256, 150);

  println("Available serial ports:");
  println(Serial.list());

  // Uses the first port in this list (number 0).  Change this to
  // select the port corresponding to your Arduino board.  The last
  // parameter (e.g. 9600) is the speed of the communication.  It
  // has to correspond to the value passed to Serial.begin() in your
  // Arduino sketch.
  port = new Serial(this, "COM3", 9600);  

  // If you know the name of the port used by the Arduino board, you
  // can specify it directly like this.
  //port = new Serial(this, "COM1", 9600);
}

void draw() {
  // draw a gradient from black to white
  for (int i = 0; i < 256; i++) {
    stroke(i);
    line(i, 0, i, 150);
  }

  // write the current X-position of the mouse to the serial port as
  // a single byte
  port.write(mouseX);
}

So does it sends data differently??? Then serial monitor???

// write the current X-position of the mouse to the serial port as
// a single byte

How can you do it in lets say vb.net

So does it sends data differently??? Then serial monitor???

Yes! The Processing you show sends a binary value, with a range of 0..255
The serial monitor sends ASCII characters.

You mean it actually sends it in 0 -11111111 form? So if i will send this string from VB it will automatically understand it?

Yes.

So if i will send this string from VB

Though don't use the word "string".

I found it better to use processing over the serial monitor.

When a byte is output serially, it is just a series of HIGH/LOW square pulses that correspond to the 0s and 1s of the binary form of whatever you are sending.

Have you received 1 byte successfuly?