Set LED Brightness as a subroutine

Hello!

I am new to the Arduino environment and need some help. I am building a 7 Segment LED GPS Clock with location awareness. This will be placed in a friends motor home. I am currently trying to write a proof of concept script that would adjust the LED brightness as a background routine. I have pieced together a script from others that I have found on GitHub and the web. What I can not do is get the script to run the brightness control as a subroutine and allow the display information to function without interruption. For the test I am using an Arduino Uno R3, Adafruit 0.56 Led with backpack, so the addressing to the LED’s are using two wire analog pins 4 & 5.

Here is the hobbled code that I am using to test this function.
/***************************************************
This is a library for our I2C LED Backpacks

Designed specifically to work with the Adafruit LED 7-Segment backpacks
----> http://www.adafruit.com/products/881
----> http://www.adafruit.com/products/880
----> http://www.adafruit.com/products/879
----> http://www.adafruit.com/products/878

These displays use I2C to communicate, 2 pins are required to
interface. There are multiple selectable I2C addresses. For backpacks
with 2 Address Select pins: 0x70, 0x71, 0x72 or 0x73. For backpacks
with 3 Address Select pins: 0x70 thru 0x77

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/

// Enable one of these two #includes and comment out the other.
// Conditional #include doesn’t work due to Arduino IDE shenanigans.
#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
//#include <TinyWireM.h> // Enable this line if using Adafruit Trinket, Gemma, etc.

#include “Adafruit_LEDBackpack.h”
#include <Adafruit_GFX.h>
#include <Wire.h>
#include <SoftwareSerial.h>

Adafruit_7segment matrix = Adafruit_7segment();
int light;

// These constants won’t change:
const int sensorPin = A0; // pin that the sensor is attached to
//const int ledPin = 9; // pin that the LED is attached to

// variables:
int sensorValue = 0; // the sensor value
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
#define DimDisplay
#define SensRead

void setup() {
#ifndef AVR_ATtiny85
Serial.begin(115200);
Serial.println(“Light Sensor Test 2”);
#endif
matrix.begin(0x70);
//matrix.setBrightness(0);

// calibrate during the first five seconds
while (millis() < 5000) {
sensorValue = analogRead(sensorPin);

// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}

// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}
}

void loop() { //begin main program loop

void SensRead(); //sensor read routine name

void DimDisplay(); //display dimmer routine name

void SensRead(); { //Declaring the sensor read routine with no parameters

// read the sensor:
sensorValue = analogRead(sensorPin);

// apply the calibration to the sensor reading
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

// in case the sensor value is outside the range seen during calibration
sensorValue = constrain(sensorValue, 0, 255);

// fade the LED display using the calibrated value:

matrix.print((sensorValue) / 16, DEC);
matrix.writeDisplay();
delay(500);

Serial.println();
Serial.print(sensorValue);

void DimDisplay(); { //Declairs Dispaly Dimmer routing with no parameters
matrix.setBrightness((sensorValue) / 16);
delay(100);

return;
}
}
}

This code is not compiling at this point, I have attempted to run the two functions independently. I would like the SensRead to run continuously and have the DimDisplay update at a much slower rate. Any way I have been able to configure this, the display brightness control is all over the place, or it delays the SensRead until the DimDisplay has preformed its task.

Thanks!
Jim

void loop() {    //begin main program loop
  
  void SensRead();

That's an unusual place for a prototype.
Did you mean to call the function?

(Did you see what I did there with code tags? You could do that too)

I didn't do anything with it except copy it from your post.

Do you know how to call a function?

I did not see any changes, but I figured that I was missing some syntax or something….

What AWOL was referring to was the way that the code was displayed here on the forum. Do you see how his post displays it differently and better than yours because he used code tags ?

Ok, what am I missing here. I read the tutorial on formatting functions, applied it to my sketch and I still get errors:
"error:expected primary-expression before ‘)’ token on lines 73, 75, 77 and 91

Light_Sensor_Test2.ino (3.07 KB)

Please, just post your code between code tags.
My phone doesn't know how to read .ino files, and I can't be bothered to tell it how to.

Ok

I'll try it again, thanks for the guidance.

void loop() {    //begin main program loop
  //main program loop
  SensRead();  //sensor read routine name

  DimDisplay();     //display dimmer routine name
}
void SensRead() {     //Declaring the sensor read routine with no parameters
  // read the sensor: 
     sensorValue = analogRead(sensorPin);
  // apply the calibration to the sensor reading
     sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
  // in case the sensor value is outside the range seen during calibration
     sensorValue = constrain(sensorValue, 0, 255);
     matrix.print((sensorValue) / 16, DEC);
     matrix.writeDisplay();
     delay(500);
     Serial.println();  
     Serial.print(sensorValue);
}

void DimDisplay() {     //Declairs Dispaly Dimmer routing with no parameters
 matrix.setBrightness((sensorValue) / 16);
  delay(100);
 }/code]

Would you edit you post and include all your sketch please?

I am getting the following error messages on the function calls, SensRead(), DimDisplay(), void SensRead() and DimDisplay().
error: expected primary-expression before ‘)’ token

/*************************************************** 
  This is a library for our I2C LED Backpacks

  Designed specifically to work with the Adafruit LED 7-Segment backpacks 
  ----> http://www.adafruit.com/products/881
  ----> http://www.adafruit.com/products/880
  ----> http://www.adafruit.com/products/879
  ----> http://www.adafruit.com/products/878

  These displays use I2C to communicate, 2 pins are required to 
  interface. There are multiple selectable I2C addresses. For backpacks
  with 2 Address Select pins: 0x70, 0x71, 0x72 or 0x73. For backpacks
  with 3 Address Select pins: 0x70 thru 0x77

  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/

// Enable one of these two #includes and comment out the other.
// Conditional #include doesn't work due to Arduino IDE shenanigans.
#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
//#include <TinyWireM.h> // Enable this line if using Adafruit Trinket, Gemma, etc.

#include "Adafruit_LEDBackpack.h"
#include <Adafruit_GFX.h>
#include <Wire.h>
#include <SoftwareSerial.h>

Adafruit_7segment matrix = Adafruit_7segment();
int light;

// These constants won't change:
const int sensorPin = A0;    // pin that the sensor is attached to
//const int ledPin = 9;        // pin that the LED is attached to

// variables:
int sensorValue = 0;         // the sensor value
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value
#define DimDisplay
#define SensRead

void setup() {
#ifndef __AVR_ATtiny85__
  Serial.begin(115200);
  Serial.println("Light Sensor Test 2");
  #endif
  matrix.begin(0x70);
  //matrix.setBrightness(0);

     // calibrate during the first five seconds 
       while (millis() < 5000) {
         sensorValue = analogRead(sensorPin);

    // record the maximum sensor value
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    }

    // record the minimum sensor value
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }
  }
}

void loop() {    //begin main program loop
  //main program loop
  SensRead();  //sensor read routine name

  DimDisplay();     //display dimmer routine name
}
void SensRead() {     //Declaring the sensor read routine with no parameters
  // read the sensor: 
     sensorValue = analogRead(sensorPin);
  // apply the calibration to the sensor reading
     sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
  // in case the sensor value is outside the range seen during calibration
     sensorValue = constrain(sensorValue, 0, 255);
     matrix.print((sensorValue) / 16, DEC);
     matrix.writeDisplay();
     delay(500);
     Serial.println();  
     Serial.print(sensorValue);
}

void DimDisplay() {     //Declairs Dispaly Dimmer routing with no parameters
 matrix.setBrightness((sensorValue) / 16);
  delay(100);
 }
/code]

Try:

/*************************************************** 
  This is a library for our I2C LED Backpacks

  Designed specifically to work with the Adafruit LED 7-Segment backpacks 
  ----> http://www.adafruit.com/products/881
  ----> http://www.adafruit.com/products/880
  ----> http://www.adafruit.com/products/879
  ----> http://www.adafruit.com/products/878

  These displays use I2C to communicate, 2 pins are required to 
  interface. There are multiple selectable I2C addresses. For backpacks
  with 2 Address Select pins: 0x70, 0x71, 0x72 or 0x73. For backpacks
  with 3 Address Select pins: 0x70 thru 0x77

  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/

// Enable one of these two #includes and comment out the other.
// Conditional #include doesn't work due to Arduino IDE shenanigans.
#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
//#include <TinyWireM.h> // Enable this line if using Adafruit Trinket, Gemma, etc.

#include "Adafruit_LEDBackpack.h"
#include <Adafruit_GFX.h>
#include <Wire.h>
#include <SoftwareSerial.h>

Adafruit_7segment matrix = Adafruit_7segment();
int light;

// These constants won't change:
const int sensorPin = A0;    // pin that the sensor is attached to
//const int ledPin = 9;        // pin that the LED is attached to

// variables:
int sensorValue = 0;         // the sensor value
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value


// ?????????????????????????????????????????????????????????????????????
//Remove these  lines
//#define DimDisplay  
//#define SensRead
// ?????????????????????????????????????????????????????????????????????


void setup() {
#ifndef __AVR_ATtiny85__
  Serial.begin(115200);
  Serial.println("Light Sensor Test 2");
  #endif
  matrix.begin(0x70);
  //matrix.setBrightness(0);

     // calibrate during the first five seconds 
       while (millis() < 5000) {
         sensorValue = analogRead(sensorPin);

    // record the maximum sensor value
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    }

    // record the minimum sensor value
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }
  }
}

void loop() {    //begin main program loop
  //main program loop
  SensRead();  //sensor read routine name

  DimDisplay();     //display dimmer routine name
}
void SensRead() {     //Declaring the sensor read routine with no parameters
  // read the sensor: 
     sensorValue = analogRead(sensorPin);
  // apply the calibration to the sensor reading
     sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
  // in case the sensor value is outside the range seen during calibration
     sensorValue = constrain(sensorValue, 0, 255);
     matrix.print((sensorValue) / 16, DEC);
     matrix.writeDisplay();
     delay(500);
     Serial.println();  
     Serial.print(sensorValue);
}

void DimDisplay() {     //Declairs Dispaly Dimmer routing with no parameters
 matrix.setBrightness((sensorValue) / 16);
  delay(100);
 }

LarryD - Thanks that fixed the compiling issue. However, this has not fixed the root issue that I am trying to resolve. What I would like to have is the SensorRead running freely and reporting the sensor value every 500 milliseconds and the DimDisplay running every 30 to 60 seconds. with the corrections listed above, I lengthened the DimDisplay to 60000 (60 seconds) and this also slowed down the SensorRead to 60 seconds. Any words of advise here?

Any words of advise here?

Three words concatenated into one. BlinkWithoutDelay, as in the example in the IDE.

Yes learn how to use BlinkWithoutDelay

Here is an example, do you understand how it works?

const unsigned long TaskAtime  = 500UL;   //Runs TaskA every 500 milli seconds
const unsigned long TaskBtime  = 60000UL; //Runs TaskB every 60 seconds

unsigned long TimeA;                      //Times up, Task A time
unsigned long TimeB;                      //Times up, Task B time

//========================================================== 
void setup()
{
  TimeA = millis();                      //Initailize  
  TimeB = TimeA;                         //Initialize

} //        >>>>>>>>>>>>>> END OF setup() <<<<<<<<<<<<<<<<<

void loop()
{
  unsigned long millisNow = millis();

  //==================
  if (millisNow - TimeA >= TaskAtime) //Is it time to run Task A?
  {
    TimeA = millis();                 //Re-initialize
    TaskA();
  } 

  //==================
  if (millisNow - TimeB >= TaskBtime) //Is it time to run Task B?
  {
    TimeB = millis();                 //Re-initialize
    TaskB();
  }  

  //Other stuff goes here


} //        >>>>>>>>>>>>>> END OF loop() <<<<<<<<<<<<<<<<<


// FUNCTIONS
//========================================================== 

void TaskA()
{
   // TaskA code stuff goes here
}

//==================
void TaskB()
{
  // TaskB code stuff goes here
}

//==================