Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7852443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:25:08+00:00 2026-06-02T19:25:08+00:00

I am pretty inexperienced with C++ however I need to use it for this

  • 0

I am pretty inexperienced with C++ however I need to use it for this project I am working on. I am using and Arduino Mega to read and log an LED array. I got the code working serially, but now I am trying to tidy it up a bit by defining my own library and classes. When I try and pass simple data like pin numbers and timeouts using accessors, They allways return 0. Or that seems to be the case when I try and print them on the serial port to test. Here is my code

SedimentLogger.h

#ifndef SEDIMENTLOGGER_H
#define SEDIMENTLOGGER_H

#include "/home/marrabld/Programming/Arduino/arduino-1.0/libraries/QTRSensors/QTRSensors.h"
#include <Arduino.h>


#define NUM_SENSORS 8
//==================================================
// This header should hold 3 small classes used for :
//--------------------------------------------------
// 1. sedimentSensor
//     Dealing with sensorHardware
// 2. sedimentWriter
//     Reading and writing data
// 3. sedimentTimer
//     Reading and writing to the RTC
//--------------------------------------------------
// 
//==================================================

class sedimentSensor
{
 public:
  QTRSensorsRC sensor1;
  QTRSensorsRC sensor2;
  sedimentSensor(); //default constructor
  ~sedimentSensor();
  void  calibrateSensors();
  unsigned int *SensorValues1(){return sensorValues1;};
  unsigned int *SensorValues2(){return sensorValues2;};
  int SampleDelay(){return sampleDelay;};
  int EmitterPin1();


 private:
  // Map pins to LED numbers
  // first LED array
  int led1;
  int led2;
  int led3;
  int led4;
  int led5;
  int led6;
  int led7;
  int led8;
  int emitterPin1;
  //second Array
  int led9;
  int led10;
  int led11;
  int led12;
  int led13;
  int led14;
  int led15;
  int led16;
  int emitterPin2;

  int  timeout; // waits for 2.5 seconds for sensor outputs to go low
  long sampleDelay; // 1 second
  int numSensors;

  unsigned int sensorValues1[];
  unsigned int sensorValues2[];

};

sedimentLogger.cpp

#include "sedimentLogger.h"
#include <Arduino.h>
#include <QTRSensors.h>
//#include "/home/marrabld/Programming/Arduino/arduino-1.0/libraries/QTRSensors/QTRSensors.h"
sedimentSensor::sedimentSensor()
{
  //Constructor
  int led1 = 24;
  int led2 = 26;
  int led3 = 28;
  int led4 = 30;
  int led5 = 32;
  int led6 = 34;
  int led7 = 36;
  int led8 = 38;
  int emitterPin1 = 22;
  //second Array
  int led9 = 25;
  int led10 = 27;
  int led11 = 29;
  int led12 = 31;
  int led13 = 33;
  int led14 = 35;
  int led15 = 37;
  int led16 = 39;
  int emitterPin2 = 23;

  int  timeout = 2500; // waits for 2.5 seconds for sensor outputs to go low
  int sampleDelay = 1000; // 1 second

  QTRSensorsRC sensor1 ((unsigned char[]) {led1,led2,led3,led4,led5,led6,led7,led8},numSensors, timeout,emitterPin1);
  QTRSensorsRC sensor2 ((unsigned char[]) {led9,led10,led11,led12,led13,led14,led15,led16},numSensors,timeout,emitterPin2);

  unsigned int sensorValues1[NUM_SENSORS];
  unsigned int sensorValues2[NUM_SENSORS];
};

sedimentSensor::~sedimentSensor()
{
};

int sedimentSensor::EmitterPin1()
{return emitterPin1;};

void sedimentSensor::calibrateSensors()
{
  Serial.begin(9600);

  unsigned int _delay = 500;
  delay(_delay);
  pinMode(13,OUTPUT);
  for(int i = 0; i<400; i++)
    {
      sensor1.calibrate();
      sensor2.calibrate();
    }
  digitalWrite(13,LOW);

  //TODO WRITE MAX AND MIN VALS TO FILE
  Serial.print("Array 1 Min :: ");
  for (int i = 0; i < NUM_SENSORS; i++)
  {
    Serial.print (sensor1.calibratedMinimumOn[i]);
    Serial.print(' ');
  }
  Serial.println();
  Serial.print("Array 2 Min :: ");
  for (int i = 0;i< NUM_SENSORS; i++)
  {
    Serial.print (sensor2.calibratedMinimumOn[i]);
    Serial.print(' ');
  }

  Serial.println();
  Serial.print("array 1 Max :: ");
  // print the calibration maximum values measured when emitters were on
  for (int i = 0; i < NUM_SENSORS; i++)
  {
    Serial.print(sensor1.calibratedMaximumOn[i]);
    Serial.print(' ');
  }
  Serial.println();
  Serial.print("array 2 Max :: ");
  // print the calibration maximum values measured when emitters were on
  for (int 
  i = 0; i < NUM_SENSORS; i++)
  {
    Serial.print(sensor2.calibratedMaximumOn[i]);
    Serial.print(' ');
  }

  Serial.println();
  Serial.println();
  delay(_delay); 

};

Main.ino

#include <QTRSensors.h>
#include <Arduino.h>
//#include "/home/marrabld/Programming/Arduino/arduino-1.0/libraries/QTRSensors/QTRSensors.h"
#include "sedimentLogger.h"
#include <Wire.h>
#include "/home/marrabld/Programming/Arduino/arduino-1.0/libraries/RTClib/RTClib.h"

sedimentSensor sedLog;//= sedimentSensor();

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

void loop()
{
  unsigned int position1 = sedLog.sensor1.readLine(sedLog.SensorValues1()); // guessing its passing SensorValues as 0s
  unsigned int position2 = sedLog.sensor2.readLine(sedLog.SensorValues2());

  unsigned char i;
  for (i = 0; i < NUM_SENSORS; i++)
    {
      Serial.print(sedLog.SensorValues1()[i] * 10/10001);      
      Serial.print(' ');
    }
  Serial.println(position1);
  for (i=0;i < NUM_SENSORS; i++)
    {
      Serial.print(sedLog.SensorValues2()[i]*10/1001);
      Serial.print(' ');
    }
  Serial.print("  ");
  Serial.println(position2);
  delay(sedLog.SampleDelay()); // No delay

  Serial.println(sedLog.SampleDelay()); // prints zero and shouldn't
  Serial.println(sedLog.EmitterPin1()); // prints zero and shouldn't
}

The calibrate sensors gets called as the LED turns on briefly as it should. But even the calibrated values that print out the serial port are junk. If I write all of this code in the main loop it works as expected.

any help understanding what is going wrong would be appreciated. thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:25:11+00:00Added an answer on June 2, 2026 at 7:25 pm

    In your sedimentSensor constructor, you are declaring new local variables with the same names as the member variables.
    Remove the types from them to turn into assignments to the members.
    Also put the NUM_SENSORS in the array declarations in the class definition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pretty self evident question...When using .push() on an array in javascript, is the object
I'm am pretty inexperienced with c++, but could use some help. I'm trying to
I'm pretty inexperienced in PHP so this may be obvious to some of you,
Pretty much I need to detect when the scrollView is at 320 x to
Pretty confused right now, while i feel i know a bit about vhosts this
Pretty simple: I need a script that reads the element ij of TWO images
Pretty sure this has been asked already, but I don't know what to search
pretty new to HTML and I was wondering why this snippet of code doesn't
I've worked with C# for many years but I'm pretty inexperienced when it comes
Pretty green on this. Any ideas why this works: $('.myclass div').append(' <a >HELLO WORLD</a>').click(this.function);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.