Home > other >  How to read from two serial monitors at a time?
How to read from two serial monitors at a time?

Time:10-30

Here's my code:

const int AnalogInPin1 = A1;
const int AnalogInPin2 = A2;
int SerialPrint = 0;
int SerialMonitor = 0;

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

}

void loop() {
  SerialPrint = analogRead(AnalogInPin1);
  Serial.print("Sensor = ");
  Serial.println(SerialPrint);

  if (SerialPrint > 400) {
   digitalWrite(3, LOW);
   digitalWrite(4, HIGH); 
   delay(500);
  }

  else{
   digitalWrite(4, HIGH); 
   digitalWrite(3, LOW); 
   delay(500);
  }
}

void setup1() {
    Serial.begin();

}

void loop1() {
  SerialMonitor = analogRead(AnalogInPin2);
  Serial.print("Sensor = ");
  Serial.println(SerialMonitor);

  if (SerialMonitor > 400) {
   digitalWrite(6, LOW);
   digitalWrite(5, HIGH);
   delay(500);
  }

  else{
   digitalWrite(5, HIGH);
   digitalWrite(6, LOW);
   delay(500);
  }
}

I am trying to make a car guidance system like the ones found in underground parking lots. and I am trying to have two serial monitors read from 2 sensors.

f�������x怘�f�������x怘

The characters above are looping in the serial monitor whilst using 19200 baud but not in 9600 baud why is that?

CodePudding user response:

If your question is about how to display two analogRead values in one SerialMonitor sketch, this should compile and work (if your SerialMonitor is set to 9600, of course):

const int AnalogInPin1 = A1;
const int AnalogInPin2 = A2;
int value1 = 0;
int value2 = 0;

void setup() {
  Serial.begin(9600);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop() {
  value1 = analogRead(AnalogInPin1);
  Serial.print("Sensor1 = ");
  Serial.print(value1);

  if (value1 > 400) {
   digitalWrite(3, LOW);
   digitalWrite(4, HIGH); 
  }
  else{
   digitalWrite(4, HIGH); 
   digitalWrite(3, LOW); 
  }

  value2 = analogRead(AnalogInPin2);
  Serial.print("\t Sensor2 = ");
  Serial.println(value2);

  if (value2 > 400) {
   digitalWrite(6, LOW);
   digitalWrite(5, HIGH);
  }
  else{
   digitalWrite(5, HIGH);
   digitalWrite(6, LOW);
  }
  delay(500);
}

I left most of your code as is, just made it compile and work. If this is not what you want, please edit your question.

  • Related