How can I only read 2nd character of my input in Serial?

Let's say I serial print "hello", How to serial read only the second letter "e" in "Hello"?

Any Ideas?

Do a read, do nothing with that character, then do another read maybe?

But having read your question again, if you're doing a serial print of something (which is outbound so to speak) what do you mean by doing a read of that, which is inbound?

if you print it's gone. The read can only be done on the destination device. Is that what you are asking for?

serial print = gives provided output in serial monitor
serial read = reads all the values outputted in the serial monitor

Is this what u asked for?

:question: :question: :question:

Serial.read() reads one byte (packed into the LSB of an int) coming into the Serial port, whatever it's connected to or returns -1 if there was nothing to read

Could this be an x-y problem? Perhaps @homechennai you could provide some detail of what you're actually trying to do.

assuming you mean the 2nd character of any "line" received

e - hello
h - charlie
u - duck

char buf [80];

void
loop (void)
{
    if (Serial.available())  {
        int n = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
        buf [n] = '\0';
        Serial.print (buf [1]);
        Serial.print (" - ");
        Serial.println (buf);
    }
}

void
setup (void)
{
    Serial.begin (9600);
}
//6=RightPWM
//5=LeftPWM
//10=RightControlCW
//11=RightControlACW
//4=LeftControlCW
//3=LeftControlACW

bool data;

int Rctrl = 11;
int Lctrl = 3;
int Rpwm = 6;
int Lpwm = 5;

int RpwmVal = 255;
int LpwmVal = 255;

int distance = 100;

void setup() {
Serial.begin(9600);

pinMode(Rctrl, OUTPUT);
pinMode(Rpwm, OUTPUT);
pinMode(Lctrl, OUTPUT);
pinMode(Lpwm, OUTPUT);
}

void loop(void) {
if (Serial.available()) {
  data = Serial.find("front");
}

Serial.print(data);

if (data == true) {
  analogWrite(Rpwm, RpwmVal);     //pwm
  analogWrite(Lpwm, LpwmVal);
  
  digitalWrite(Rctrl, HIGH);      //control
  digitalWrite(Lctrl, HIGH); 

  delay(341*0.047506*distance*1.2);

  digitalWrite(Rctrl, LOW);
  digitalWrite(Lctrl, LOW);
  
  delay(1000);
  data = false;
}
else {
delay(1000);
}
}

See, Im trying to make a arduino car where in if I type front100 in serial monitor, It travels FRONT - 100cm

What I have coded so far :
If I type "front" in the serial monitor, It goes front.

What I can't code :
If I type "front100", the last 3 digits are considered as distance and it travels the distance.

So it was an xy issue :wink:

Have a look at Robin2's serial tutorial for some ideas on how to structure and read data.

edit: why did you mark one post as a solution, but then still say in the next post you don't know how to do it?

its a DELAY. Thanks for help anywaysss :smile:

what about?
adding a space before the decimal makes it easier to recognize


char buf [80];

void
loop (void)
{
    if (Serial.available())  {
        int n = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
        buf [n] = '\0';

        char cmd [20];
        int  val;
        sscanf (buf, "%s %d", cmd, &val);

        Serial.print ("  ");
        Serial.print (cmd);
        Serial.print ("  ");
        Serial.println (val);
    }
}

void
setup (void)
{
    Serial.begin (9600);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.