Issue with Serial communication

Hello,

I'm hoping this isn't a stupid question, but I can't seem to find the answer, so I'm hoping you can give me a hand.

I have a sketch that communicates with the pc through the serial monitor. For a while it ran just fine, but now the response is slow or non-existent. I think it's the serial communication, but I don't know where

I get some output, but nothing more:

Serial Ready, Run Multiple VCNL Scanner

TCA Port #0
TCA Port #1
Found I2C 0x13
VCNL4010 test
Found VCNL4010
TCA Port #2
Found I2C 0x13
VCNL4010 test
Found VCNL4010
TCA Port #3
Found I2C 0x13
VCNL4010 test
Found VCNL4010
TCA Port #4
Found I2C 0x13
VCNL4010 test
Found VCNL4010

I'm using an Arduino mega and Arduino IDE 1.8.2

Lot's of thanks in advance!

#define TCAADDR 0x70

#include <SPI.h>
#include <Wire.h>
extern "C" { 
#include "utility/twi.h"  // from Wire library, so we can do bus scanning
}
//#include <Adafruit_GFX.h>
//#include <Adafruit_SSD1306.h>
#include "Adafruit_VCNL4010.h"


Adafruit_VCNL4010 vcnl;

//Variables
const uint8_t nrVCNL = 6;
float proximityValue[]= {}; 



void setup() {
  // put your setup code here, to run once:
    Serial.begin(115200); //115200
    while (!Serial);
    delay(1000);

    Wire.begin();

    Serial.println("Serial Ready, Run Multiple VCNL Scanner \n");

     for (uint8_t t=0; t<8; t++) {
      tcaselect(t);
      Serial.print("TCA Port #"); Serial.println(t);

      for (uint8_t addr = 0; addr<=127; addr++) {
        if (addr == TCAADDR) continue;
      
        uint8_t data;
        if (! twi_writeTo(addr, &data, 0, 1, 1)) {
           Serial.print("Found I2C 0x");  Serial.println(addr,HEX);
           Serial.println("   VCNL4010 test");
             if (! vcnl.begin()){
               Serial.println("   Sensor not found :(");
                 while (1);
                 }
              Serial.println("   Found VCNL4010");
        }
      }
    }
    Serial.println("\n Multiple VCNL Scanner done");

    for (uint8_t i = 1; i<=nrVCNL; i++) {
    proximityValue[i] = 0;
     }

}

void loop() {
  // put your main code here, to run repeatedly:
  for (uint8_t i = 1; i<=nrVCNL; i++) {
    tcaselect(i);
    proximityValue[i] = vcnl.readProximity();
  }

  Serial.println("Sensor Proximity Values:");
  for (uint8_t i = 1; i<=nrVCNL; i++) {
    Serial.print("  #"); Serial.print(i); Serial.print(":"); Serial.print(proximityValue[i]);
  }
  Serial.println("\n");
  
delay(5000);
}


 
void tcaselect(uint8_t i) {
  if (i > 7) return;
 
  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();  
}

TestMagnet.ino (1.79 KB)

What is a "VCNL Scanner"?

Why do you think the problem may be with the serial communication? What tests have you carried out?

If your program was working properly and is now not working the same what has changed?

...R

Hello,

Thank you for responding. 'VCNL scanner' is the name I've given for that part in the sketch. It's a code modification of a multiplex i2c scanner that also checks whether the connected VCNL4010 sensor is responding.

After the setup it continuously reads these sensors and outputs the data onto the serial monitor

I didn't change anything to the code. I was running the code and getting the values for about an hour until it just seemed to freeze and it stopped displaying values ever since.

I've also tried running an other sketch, one about serial monitors got from Instructables.com. And the monitor froze as well. So I think the problem is there, but don't know where.

Connect just one and see if you are able to communicate.

laura85:
I've also tried running an other sketch, one about serial monitors got from Instructables.com. And the monitor froze as well. So I think the problem is there, but don't know where.

I don't wish to sound negative but I cannot figure out from that sentence what the word "there" refers to.

...R

float proximityValue[]= {};

The compiler is being asked to determine the size of the array based on the number of initializers. If quickly counted to ZERO, so it produced an array that can hold nothing.

Not all that useful, eh?

  for (uint8_t i = 1; i<=nrVCNL; i++) {
    tcaselect(i);
    proximityValue[i] = vcnl.readProximity();
  }

And, yet, you try to write to positions 1, 2, 3, 4, 5, and 6 of that array that can hold ZERO elements. Unpredictable things can happen when you do stupid stuff like that.

It's a wonder Trump didn't get elected president.

Oh, wait. He did, and it's your fault.

Hey guys,

Sorry for the late response. I was occupied with stuff.

@Robin2
Don't worry, I don't think you sound negative, I should have articulated it better. 'There' meant I thought the problem is with the serial communication, because I got the same problem with that other sketch tgat solely focused on the serial com.

@PaulS
Thanx, I hadn't noticed that. The compiler didn't warn me either. I'll change it to

"float proximityValue[nrVCNL];"

I've tried this sketch as well, which I got from Instructables.

/* YourDuinoStarter_SerialMonitor_SEND_RCVE
 - WHAT IT DOES: 
   - Receives characters from Serial Monitor
   - Displays received character as Decimal, Hexadecimal and Character
   - Controls pin 13 LED from Keyboard
 - SEE the comments after "//" on each line below
 - CONNECTIONS:
   - None: Pin 13 built-in LED
   - 
 - V1.00 02/11/13
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
/*-----( Declare Constants and Pin Numbers )-----*/
#define led 13  // built-in LED
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
int ByteReceived;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);  
  Serial.println("--- Start Serial Monitor SEND_RCVE ---");
    Serial.println(" Type in Box above, . ");
  Serial.println("(Decimal)(Hex)(Character)");  
  Serial.println(); 
}
//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  if (Serial.available() > 0)
  {
    ByteReceived = Serial.read();
    Serial.print(ByteReceived);   
    Serial.print("        ");      
    Serial.print(ByteReceived, HEX);
    Serial.print("       ");     
    Serial.print(char(ByteReceived));
    
    if(ByteReceived == '1') // Single Quote! This is a character.
    {
      digitalWrite(led,HIGH);
      Serial.print(" LED ON ");
    }
    
    if(ByteReceived == '0')
    {
      digitalWrite(led,LOW);
      Serial.print(" LED OFF");
    }
    
    Serial.println();    // End the line

  // END Serial Available
  }
}

//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

/*********( THE END )***********/

And this is all that comes out on the serial monitor:

--- Start Serial Monitor SEND_RCVE ---
Type in Box above, .
(Decimal)(Hex)(Character)

Which is the same behavior I get with my sketch: A few initial lines that come slow, then nothing. And this sketch only involves the serial communication, so that must be the problem.

laura85:
--- Start Serial Monitor SEND_RCVE ---
Type in Box above, .
(Decimal)(Hex)(Character)

Which is the same behavior I get with my sketch: A few initial lines that come slow, then nothing. And this sketch only involves the serial communication, so that must be the problem.

Are you saying that if you type a character in the Serial Monitor there is no response from the program?

The program worked for me except that the LED would not switch because this line

pinMode(led, OUTPUT);

was missing from setup()

...R

De sketch doet het nu wel inderdaad.

Een collega heeft er even naar gekeken, hij vermoedt dat het aan de hardware ligt, dat de Multiplexer kapot is. Ik ga het straks even proberen met een reserve onderdeel.

laura85:
De sketch doet het nu wel inderdaad.

English, please.

...R

Sorry...

That other sketch is actually working now. So maybe the serial com isn't the issue after all.

I asked a colleague and he thinks it might be the hardware. Possibly the multiplexer is broken. I'm gonna try with a spare, after I've soldered it together.