Using two functions in one class not working. Variable 'not declared in scope'.

Here is a snippet of the problem area. What I am trying to do, is control a series of relays with the serial terminal. I have that part working correctly, but I want to add the ability to have each relay turn on and off at different rates, individually, controlled also from the serial terminal.

Currently, I am able to change the onTime and offTime from the terminal, but only if it's a global variable, so every relay follows the same onTime and offTime. I also am able to get it to work buy using a different global variable for each relay, but that adds way too many unnecessary lines of code while also limiting the flexibility. Currently I'm running 8 relays connected to various things.

I am open to suggestions of a better way to do this, but here is what I am trying to do now. Keep in mind, this is only a small test version I'm using in order to try and figure out how to change the onTime, offTime, ledPin, and analog input from within the loop.

class Test
{
    int ledPin;
    int photoPin;
    long onTime;
    long offTime;
    int ledState = 0;
    int lightReading;
    unsigned long lightStart;
    unsigned long lightStop;
    unsigned long nextChangeTime = 0;
  public:
    Test(int led, int photo, int on, int off) 
    {
      ledPin = led;
      pinMode(ledPin, OUTPUT);
      photoPin = photo;
      pinMode(photoPin, INPUT);
      ledState = LOW;
      onTime = on;
      offTime = off;
      unsigned long currentTime;
    }
    void Lightme() 
    {
      unsigned long currentTime = millis();
      if (currentTime >= nextChangeTime) {
        nextChangeTime=(millis()+onTime);
        if (ledState == LOW) {
          ledState = HIGH;
        } else {
          ledState = LOW;
        }
      }
      digitalWrite(ledPin, ledState);
    }
  void Update(int led, int photo, int on, int off)
  {
     
    OnTime = on;
    OffTime = off;

  }
};

void setup() {
  Serial.begin(9600);
  pinMode(7, OUTPUT);
}

Test sample1 = Test(7,A0,100,250);

void loop() {
  sample1.Update(7,A0,1000,1000);
  sample1.Lightme();
}
    long onTime;
    long offTime;
   OnTime = on;
    OffTime = off;

The compiler cares about case. You should too.

Also, don't call pinMode in your constructor. Rather, include a 'begin()' method that can be called from setup().

Also, this will fail when spanning a millis() rollover:

if (currentTime >= nextChangeTime) {

Also, onTime and offTime should be unsigned long, not long.

wow! Long day and totally missed that I mismatched the case! Thank you! Also looking into the other suggestions you mentioned for tomorrow.

Warning: Your constructor calls pinMode() (twice) but the Arduino runtime library is not initialized until AFTER the global objects are initialized. Move the Arduino calls to a 'begin' method and call it from setup().

class Test
{
    int ledPin;
    int photoPin;
    unsigned long onTime;
    unsigned long offTime;
    int ledState;
    int lightReading;
    unsigned long lightStart;
    unsigned long lightStop;
    unsigned long lastChangeTime;


  public:
    Test(int led, int photo, unsigned long on, unsigned long off)
    {
      ledPin = led;
      photoPin = photo;
      ledState = LOW;
      onTime = on;
      offTime = off;
      lastChangeTime = 0;
    }


    void begin()
    {
      pinMode(ledPin, OUTPUT);
      pinMode(photoPin, INPUT);
    }


    void Lightme()
    {
      unsigned long currentTime = millis();
      if (currentTime - lastChangeTime >= onTime)
      {
        lastChangeTime = currentTime;
        ledState =  (ledState == LOW) ? HIGH : LOW;
      }
      digitalWrite(ledPin, ledState);
    }


    void Update(unsigned long on, unsigned long off)
    {
      onTime = on;
      offTime = off;
    }
};




Test sample1 = Test(7, A0, 100, 250);




void setup()
{
  Serial.begin(9600);
  pinMode(7, OUTPUT);
  sample1.begin();
}


void loop()
{
  sample1.Update(1000, 1000);
  sample1.Lightme();
}

Very helpful Johnwasser, Thank you! I have been sort of crash coursing myself through learning to program for about the last year on and off, and I often get ahead of myself, and therefore lost.

johnwasser:
Warning: Your constructor calls pinMode() (twice) but the Arduino runtime library is not initialized until AFTER the global objects are initialized. Move the Arduino calls to a 'begin' method and call it from setup().

ledState =  (ledState == LOW) ? HIGH : LOW;

I'm interested in what's going on here if you have time to explain. I did a quick search but I'm curious about the fundamentals behind it, so I can use it in other parts of my larger program.
Thanks!

rpc1986:
I'm interested in what's going on here if you have time to explain. I did a quick search but I'm curious about the fundamentals behind it, so I can use it in other parts of my larger program.
Thanks!

Ternary Operator