Using the Serial Monitor with Serial.read() and Serial.parseInt() in the same code

Hello, I was wondering if anyone could help with a problem with the serial monitor

In this project I have a master Arduino that I am reading the value ADC A0 value from a 10k potentiometer (adjusts the voltage between 0 - 3.3V) and sends the value over serial0 to a slave Arduino Due. The slave Arduino Due, takes the value it receives and outputs it on the DAC in the voltage range 0.55 - 2.7 V). This works ok

However, I would like to display the incoming serial value on the slave Due's on the serial monitor. When I try to do this nothing appears on the slave's serial monitor. I think it might have something to do with the line: int val = Serial.parseInt(); which is what I use to convert the incoming serial chars into ints for the DAC. When I remove this line I am able to see the value on the serial monitor, but I lose the ability to output the value on the slave's DAC

I have looked all over the internet for a solution without much luck.

Can anyone help with finding away to output the 0.55 - 2.75V on the DAC whilst monitoring the incoming serial values on the serial monitor?

Many thanks in advance

My code for the master and slave Arduinos can be seen below:

Master Arduino

void setup()
{
   // initalize serial communication at 9600 bits per second:
  Serial.begin(115200);

}

//the loop routine runs over and over again forever:

void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);

  Serial.println(sensorValue);

  delay(30);  //delay in between reads for stability

}

Slave Arduino

int val;

void setup()
{
  Serial.begin(115200);
  analogWriteResolution(8);
  pinMode(DAC0, OUTPUT); 
}

void loop()
{
  char z;
  int val = Serial.parseInt();
  
  if (Serial.available()>0)
  {
    z = Serial.read();
    Serial.print(z);
    analogWrite(DAC0, val);
  }
  
}

Try:

void loop()
{
   if (Serial.available() > 0)
   {
      int val = Serial.parseInt();
      Serial.print(val);
      analogWrite(DAC0, val);
   }
}

The serial input basics tutorial may be of interest.

1 Like

Thank you very much!

This did the trick! As my username suggests I am still very much a beginner.

I am also looking through the tutorial which is very useful!

Glad that I could help.

I use the methods in that tutorial for all of my code that does serial communication.

1 Like

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