Serial monitor

Hi - I got this sketch from a website. It's used for controlling LED's on a Scalextric race track.

The code works fine - if I connect the LED's and run the sketch, the LED's turns on and off as expected.

But there is a lot more data then used in the sketch and I would like to see the data in the Serial Monitor if possible?

Can someone give me a snip of code that shows all data in the Serial Monitor? Every string starts with [ and ends with ] and in between is the data I need

Used Sketch:

//start
// define which pins are to be used for the various light functions
#define GREEN1 12 //edit for your green pin
#define AMBER1 11 //edit for your amber/yellow pin
#define RED1 10 //edit for your 5th red pin
#define RED2 9 //edit for your 4th red pin
#define RED3 8 //edit for your 3rd red pin
#define RED4 7 //edit for your 2nd red pin
#define RED5 6 //edit for your 1st red pin
#define BLUE1 5 //edit for your blue pin
int amberswPin = 4;//edit to be any unused digital pin
int blueswPin = 3;//edit to be any unused digital pin
int ledPin = 13;
unsigned long nextBlink;
char amberswState = LOW;
char blueswState = LOW;
char ledState = LOW;
int CarsInPits = 0;
// setup the pins for outputs, blinking and open the serial port
void setup()
{
pinMode(GREEN1, OUTPUT);
pinMode(RED1, OUTPUT);
pinMode(RED2, OUTPUT);
pinMode(RED3, OUTPUT);
pinMode(RED4, OUTPUT);
pinMode(RED5, OUTPUT);
pinMode(AMBER1, OUTPUT);
pinMode(BLUE1, OUTPUT);
pinMode(ledPin, OUTPUT);
amberswState = LOW;
ledState = LOW;
nextBlink = millis();
Serial.begin(9600);
}

void blink (void)
{ unsigned long now;
now = millis();
if (now >= nextBlink) {
nextBlink = now + 1000; // Next change in one second or edit value 1000 to prefered milliseconds
if (ledState == LOW) {
digitalWrite(ledPin, HIGH);
ledState = HIGH;
}
else {
digitalWrite(ledPin, LOW);
ledState = LOW;
}
}
}
// look for data on the serial port and turn on/off the lights
// data is ascii strings

void loop()

{ blink();

if (ledState == HIGH && amberswState == HIGH) {digitalWrite (AMBER1, HIGH); digitalWrite(GREEN1, LOW);}
else {digitalWrite (AMBER1, LOW);}
if (ledState == LOW && amberswState == HIGH) {digitalWrite (AMBER1, LOW);}

if (ledState == HIGH && blueswState == HIGH) {digitalWrite (BLUE1, HIGH);}
else {digitalWrite (BLUE1, LOW);}
if (ledState == LOW && blueswState == HIGH) {digitalWrite (BLUE1, LOW);}

if ( Serial.available() )
{
String b;
b = Serial.readStringUntil('[');
{ String s;

s = Serial.readStringUntil(']');
if ( s == "SL010" ) { digitalWrite(RED1, LOW); }
if ( s == "SL011" ) { digitalWrite(RED1, HIGH); }
if ( s == "SL020" ) { digitalWrite(RED2, LOW); }
if ( s == "SL021" ) { digitalWrite(RED2, HIGH); }
if ( s == "SL030" ) { digitalWrite(RED3, LOW); }
if ( s == "SL031" ) { digitalWrite(RED3, HIGH); }
if ( s == "SL040" ) { digitalWrite(RED4, LOW); }
if ( s == "SL041" ) { digitalWrite(RED4, HIGH); }
if ( s == "SL050" ) { digitalWrite(RED5, LOW); }
if ( s == "SL051" ) { digitalWrite(RED5, HIGH); digitalWrite(RED1, LOW); digitalWrite(RED2, LOW); digitalWrite(RED3, LOW); digitalWrite(RED4, LOW); digitalWrite(AMBER1, LOW); digitalWrite(GREEN1, LOW);}
if ( s == "SL060" ) { digitalWrite(GREEN1, LOW); }
if ( s == "SL061" ) { digitalWrite(GREEN1, HIGH); }
if ( s == "SL080" ) { digitalWrite(amberswPin, LOW); amberswState = LOW; digitalWrite(GREEN1, HIGH);}
if ( s == "SL081" ) { digitalWrite(amberswPin, HIGH); amberswState = HIGH;}
if ( s == "PT010" ) { CarsInPits-- ;} // A car just left the pits, minus one car from CarsInPits count
if ( s == "PT011" ) { CarsInPits++ ;} // A car just entered the pits, plus one car to CarsInPits count
if ( s == "PT020" ) { CarsInPits-- ;}
if ( s == "PT021" ) { CarsInPits++ ;}
if ( s == "PT030" ) { CarsInPits-- ;}
if ( s == "PT031" ) { CarsInPits++ ;}
if ( s == "PT040" ) { CarsInPits-- ;}
if ( s == "PT041" ) { CarsInPits++ ;}
if ( s == "PT050" ) { CarsInPits-- ;}
if ( s == "PT051" ) { CarsInPits++ ;}
if ( s == "PT060" ) { CarsInPits-- ;}
if ( s == "PT061" ) { CarsInPits++ ;}

if (CarsInPits <= 0 ) { //if there are zero cars in the pit, turn off the blue lights
digitalWrite(blueswPin, LOW); blueswState = LOW;
}
else { // if there is one or more cars in the pit , keep the blue lights going
digitalWrite(blueswPin, HIGH); blueswState = HIGH;
}
if (CarsInPits < 0 || CarsInPits > 6) {CarsInPits = 0; } //if number of cars in pits gets messed up reset to zero [ '||' = 'or' ]
//Serial.println(CarsInPits); //debug

}
}
}
//end

Assuming, well, a whole load of things (*), this might work:

...
s = Serial.readStringUntil(']');

Serial.println( s );  // add this line to write data to serial monitor

if ( s == "SL010" ) { digitalWrite(RED1, LOW); }
...

Yours,
TonyWilk

(*) It would help a lot if you posted code between code tags, gave a link to the source website and/or gave us a diagram of the whole setup - otherwise it really is guesswork.

Sorry - my mistake

Link to the source :Arduino Sketch for use with pcLapcounter | SlotForum

The setup is like this ...

The Scalextric trac controller is connected to a laptop using USB. The software to control the race (PC Lapcounter) is running on the laptop. In PC Lapcounter there is a option "Send recieve data from Arduino board" - and a selected COM port. Arduino

On the Uno board, I have connected OUT pins 5-12 to LED's as described in the code - and it works.

I have tried to insert the line suggested, but I do not get any data.

I have tried to insert the line suggested, but I do not get any data.

What baud rate do you have set in the Serial monitor ?

(deleted)

The baud rate is 9600

"You cant run the serial monitor WHILE the program is connecting to the arduino (one port = one connection)"

Should I then compile and upload - and then start the Serial Monitor og how do you mean ??

First check your serial with #1 code, on serial plotter you should see sine wave, this part of the line is responsible for serial readings; "Serial.println"
You need to use serial method of uplowding, for testing you can use second code, when you get ok with both codes you add mentioned part "Serial.println" to you program.

#1

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

void loop() {
for (int j = 0; j < 360; j++) {
Serial.println(sin(j * (PI / 180)));
}
}

#2

/*

  Smoothing

  Reads repeatedly from an analog input, calculating a running average
  and printing it to the computer.  Keeps ten readings in an array and
  continually averages them.

  The circuit:
    * Analog sensor (potentiometer will do) attached to analog input 0

  Created 22 April 2007
  By David A. Mellis  <dam@mellis.org>
  modified 9 Apr 2012
  by Tom Igoe
  http://www.arduino.cc/en/Tutorial/Smoothing

  This example code is in the public domain.


*/


// Define the number of samples to keep track of.  The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input.  Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 10;

int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = A0;

void setup() {
  // initialize serial communication with computer:
  Serial.begin(9600);
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
}

void loop() {
  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = analogRead(inputPin);
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
  }

  // calculate the average:
  average = total / numReadings;
  // send it to the computer as ASCII digits
  Serial.println(average);
  delay(1);        // delay in between reads for stability
}

When you type something in serial monitor in top window and click -send - you should see something in lower big window, that mean that you have serial connection between PC and your board, if nothing happens you need to instal driver - I dont remember the name

I think I have found the problem ...

When the sketch is uploaded and PC Lapcounter is started - the COM ports are busy and I can't communicate with the board.

Is there any other way to "see / monitor" the communication between PC Lapcounter and the board?

I would like to se the data that PC Lapcounter is sending to the board ...

Hopy anyone can help :slight_smile:

You need to use softwareSerial or use a board that has multiple hardware serial ports, like the Pro Micro, Leonardo, or Mega.

If results testing of suggested programs are positive the problem is in your program,

aarg:
You need to use softwareSerial or use a board that has multiple hardware serial ports, like the Pro Micro, Leonardo, or Mega.

I can't get softwareSerial to show data. I have found an article describing that softwareSerial is for monitoring external serial communication - not on the internal serial interface.

I have found this add-on - can I use that? : Accessories — Arduino Official Store

I'm a newbie - can't see 2 USB interfaces on MEGA?

Your request is puzzling - why do you need to monitor the data? You can see from the sketch exactly what must be going on. Is this for some kind of troubleshooting?

If you are really desperate to do this, you can attach the RX pin of a serial-USB converter to the RX/TX pins to "sniff" the traffic there.

The protocol used from PC Lapcounter is free and besides the output used in the skecth, there are other available.

One of them is Fuel Level from every of the six cars. The format is [FLnnrxxx] r = Refuel Status, 0 = not refueling, 1 = Refueling, xxx = fuel level out of 100

If the car 1 is running full speed, I would like to monitor if I get [FL010100] and then [FL010099] and so on .... or if skips some of the outputs and I only get [FL010100] and then [FL010095] and so on ...

So it would really help me and my project, if I can monitor the data on the USB / Serial port ... :slight_smile:

Sorry for not explaining the project good enough

My similar problem is gone when I installed this - look at the pic.
And I am using RX and TX pins

Sorry - what pic ????

Thanx - allready updated to that level - did not help :frowning:

aarg:
If you are really desperate to do this, you can attach the RX pin of a serial-USB converter to the RX/TX pins to "sniff" the traffic there.

What serial-USB converter will you recommend?

(deleted)