2

I'm writing a program that has a sub program that saves values to an array, but the sub program that saves the values to the array comes back with an error telling me that the array isnt declared in that scope.

I'm fairly new to this and self taught but even when I try and pass the array into the sub program I get the same error. I'm sure its a fairly simple obvious error I'm making but I just cant get it to work.

If anyone sees any other errors or better ways for me to write any of this any help would be greatly valued as I'm still not confident on what I'm supposed to be doing.

#include <HX711.h>

#include <Wire.h>  // IC2 communication

#include <LiquidCrystal_I2C.h>  //LCD
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); // Change to (0x27,16,2) for 16x2 LCD.

int max_load;
int interval;
int max_load_prev;
int interval_prev;
int mode;
int handle_weight;
int tip_weight;
int stop1;
int mode_previous;
int interval_sum;

int mode1 = 53;
int mode2 = 51;
int mode3 = 49;
int start1 = 39;
int start2 = 37;
int increment = 47;
float potentiometer1 = A0;
float potentiometer2 = A1;
int tare_1 = 45;
int tare_2 = 43;

//stepper motor controller
int stepPin = 12;
int dirPin = 13;

//sensor readings
  HX711 tip_read;
  HX711 handle_read;
  int DOUT_tip = 6;
  int DOUT_handle = 5;
  int CLK_tip = 4;
  int CLK_handle = 3;

void setup() {
  // put your setup code here, to run once:
  
  lcd.init();
  lcd.backlight();

  //saving to array//
  float data_storage_array[600][3];

  pinMode(mode1, INPUT);
  pinMode(mode2, INPUT);
  pinMode(mode3, INPUT);
  pinMode(start1, INPUT);
  pinMode(start2, INPUT);
  pinMode(increment, INPUT);
  pinMode(stop1, INPUT);
  pinMode(potentiometer1, INPUT);
  pinMode(potentiometer2, INPUT);

  //sesnor readings
  tip_read.begin(DOUT_tip, CLK_tip);
  tip_read.set_scale(55);// change to calibration factor
  handle_read.begin(CLK_tip,CLK_handle);
  handle_read.set_scale(55);// change to calibration factor

  pinMode(tare_1, INPUT);
  pinMode(tare_2, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  mode_previous = 0;
  interval = 25;
  max_load_prev = 0;
  interval_prev = 0;
  interval_sum = 0;
  while (digitalRead(start1) == 0) {
    if (digitalRead(mode1) == 1) {
      mode = 1;
    }
    if (digitalRead(mode2) == 1) {
      mode = 2;
    }
    if (digitalRead(mode3) == 1) {
      mode = 3;
    }
    if (mode_previous != mode) {
      Display_mode_set_up(mode);
      mode_previous = mode;
    }
    if (digitalRead(increment) == 1) {
      delay(500);
      if (interval >= 1000) {
         interval = 25;
      }
      else if (interval < 1000) {
        interval = interval + 25;
      }
    }
    max_load = max_load_reading();
    if (max_load_prev != max_load or interval_prev != interval){
      Display_value_set_up(max_load, interval, mode);
      max_load_prev = max_load;
      interval_prev = interval;
    }
    Display_output(handle_weight, tip_weight);
  
  tare_one();
  }
  if (mode == 1) {
    mode_one(max_load, tip_weight);
  }
  else if (mode == 2) {
    mode_two(max_load, interval, handle_weight, tip_weight);
  }
  else if (mode ==3) {
    mode_three(max_load, interval, handle_weight, tip_weight);
  }
  Pause_stop();
}

//---SUB ROUTINES---

void mode_one(int max_load, int tip_weight) {
  tip_weight = read_tip();
  while (tip_weight < max_load) {
    tip_weight = read_tip();
    turn_motor();
    Pause_stop();
    Display_output(handle_weight, tip_weight);
  }
}

void mode_two(int max_load, int interval, int handle_weight, int tip_weight) {
  int aim_value;
  aim_value = 0 + interval;
  tip_weight = read_tip;
  while (tip_weight < max_load){
    while (tip_weight < aim_value){
      turn_motor();
      Pause_stop();
      Display_output(handle_weight, tip_weight);
      tip_weight = read_tip();
    }
    save_to_array(interval_sum);
    aim_value = aim_value + interval;
    while (digitalRead(start2) == 0) {
      delay(1);
    }
  }
  send_values_to_pc();
}

void mode_three(int max_load, int interval, int handle_weight, int tip_weight) {
  int aim_value;
  aim_value = 0 + interval;
  tip_weight = read_tip();
  while (tip_weight < max_load) {
    while (tip_weight < aim_value) {
      turn_motor();
      Pause_stop();
      Display_output(handle_weight, tip_weight);
      tip_weight = read_tip();
    }
    save_to_array(interval_sum);
    aim_value = aim_value + interval;
    delay(10000);
  } 
  send_values_to_pc(); 
}

void tare_one() {
  if (tare_1 == HIGH) {
    tip_read.tare();
  }
  if (tare_2 == HIGH) {
    handle_read.tare();
  }
}

void turn_motor() {
  digitalWrite(dirPin, HIGH);
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(500);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(500);
}

int read_tip() {
  int weight;
  weight = tip_read.get_units();
  return weight;
}

int read_handle() {
  int weight;
  weight = handle_read.get_units();
  return weight;
}

void save_to_array(int interval_sum) {
  data_storage_array[interval_sum][0] = interval_sum;
  data_storage_array[interval_sum][1] = tip_read.get_units();
  data_storage_array[interval_sum][2] = handle_read.get_units();
  interval_sum = interval_sum + 1;
}

void send_values_to_pc() {
}

int max_load_reading() {
  int KG_value;
  int g_value;
  int load_max;
  KG_value = analogRead(potentiometer1);
  g_value = analogRead(potentiometer2);
  KG_value = KG_value * 2;
  KG_value = KG_value/ 100;
  KG_value = round(KG_value);
  KG_value = KG_value* 100;
  g_value = g_value/ 10;
  g_value = round(g_value);
  if (g_value >= 100){
    g_value = 99;
  }
  load_max = KG_value + g_value;
  return load_max;
}

void Pause_stop() {
  if (digitalRead(stop1) == 1){
    delay(1000);
    while (digitalRead(start1) == 0){
      if (digitalRead(stop1) == 1){
        motor_unwind();
      }
      else{
        delay(100);
        lcd.setCursor(0, 2);
        lcd.print("Program Paused");
      }      
    }
    lcd.setCursor(0, 2);
    lcd.print("                    ");
  }
}

void motor_unwind() {
  tip_weight = read_tip();
  while (tip_weight < 100) {
    digitalWrite(dirPin, LOW);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  }
  int count = 100;
  while (count > 0) {
    digitalWrite(dirPin, LOW);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
    count = count -1;
  }
}

void Display_mode_set_up(int mode) {
  lcd.setCursor(0, 0);
  if (mode == 1) {
    lcd.print("Mode: Max           ");
    lcd.setCursor(0, 1);
    lcd.print("MLoad:");
    lcd.setCursor(12, 1);
    lcd.print("inti:---");
  }
  if (mode == 2) {
    lcd.print("Mode: Incrimental    ");
    lcd.setCursor(0, 1);
    lcd.print("MLoad:");
    lcd.setCursor(12, 1);
    lcd.print("inti:   ");    
  }
  if (mode == 3) {
    lcd.print("Mode: Automatic      ");
    lcd.setCursor(0, 1);
    lcd.print("MLoad:");
    lcd.setCursor(12, 1);
    lcd.print("inti:   ");   
  }
  lcd.setCursor(0, 3);
  lcd.print("Tip:");
  lcd.setCursor(9, 3);
  lcd.print("Handle:");
}

void Display_value_set_up(int max_load, int interval, int mode) {
  if (mode == 1) {
    lcd.setCursor(6, 1);
    lcd.print("      ");
    lcd.setCursor(6, 1);
    lcd.print(max_load);
  }
  if (mode == 2 or mode == 3) {
    
    lcd.setCursor(6, 1);
    lcd.print("      ");
    lcd.setCursor(6, 1);
    lcd.print(max_load);
    
    lcd.setCursor(17, 1);
    lcd.print("   ");
    lcd.setCursor(17, 1);
    lcd.print(interval);
  }
}

void Display_output(int handle_weight, int tip_weight) {
  tip_weight = read_tip();
  handle_weight = read_handle();
  lcd.setCursor(4, 3);
  lcd.print(tip_weight);
  lcd.setCursor(16, 3);
  lcd.print(handle_weight);
}
1
  • 1
    Google “C++ scope” and do some reading about what variable scope means. It will all make sense pretty quick.
    – Delta_G
    Sep 9, 2020 at 14:40

2 Answers 2

1

You are declaring float data_storage_array[600][3] in setup(), and using it in another function, that can't "see" it.

The simplest way of making it work is making your array global by declaring it at the top in the section where you declare other variables like max_load.

0

As others have mentioned, your array is out of scope and can only be used in the setup() function. I have moved this out of the setup() function and into the global variable section.

I am also cautious of how much SRAM you were using with your (600 x 3 =) 1800 float elements. So I have changed it to ints and reduced it to 250 x 3. You have put down that you are using the Arduino Mega, so this could be expanded further if necessary.

Your variable interval_sum was set to increment with no upper limit. This means you would end up going beyond the limits of the array. I have fixed this.

I have reviewed your code briefly and added some fixes and some comments. It is untested. I hope this helps you.

#include <HX711.h>
#include <Wire.h>  // IC2 communication
#include <LiquidCrystal_I2C.h>  //LCD

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); // Change to (0x27,16,2) for 16x2 LCD.

int max_load;
int interval;
int max_load_prev;
int interval_prev;
int mode;
int handle_weight;
int tip_weight;
int stop1;
int mode_previous;
int interval_sum;

const int mode1 = 53;    // <-- added the const keyword to all pin definitions as they dont change
const int mode2 = 51;
const int mode3 = 49;
const int start1 = 39;
const int start2 = 37;
const int increment = 47;
const int potentiometer1 = A0;    // <-- Changed to int as it is only a pin definition
const int potentiometer2 = A1;    // <-- Changed to int as it is only a pin definition
const int tare_1 = 45;
const int tare_2 = 43;


//stepper motor controller
const int stepPin = 12;
const int dirPin = 13;


//sensor readings
HX711 tip_read;
HX711 handle_read;
int DOUT_tip = 6;
int DOUT_handle = 5;
int CLK_tip = 4;
int CLK_handle = 3;

//saving to array//
int data_storage_array[250][3];    // <-- Moved before setup(). Changed to int. Reduced to 250x3 elements.


void setup() {
  // put your setup code here, to run once:
  lcd.init();
  lcd.backlight();

  pinMode(mode1, INPUT);
  pinMode(mode2, INPUT);
  pinMode(mode3, INPUT);
  pinMode(start1, INPUT);
  pinMode(start2, INPUT);
  pinMode(increment, INPUT);
  pinMode(stop1, INPUT);
  pinMode(potentiometer1, INPUT);
  pinMode(potentiometer2, INPUT);

  //sesnor readings
  tip_read.begin(DOUT_tip, CLK_tip);
  tip_read.set_scale(55);// change to calibration factor

  handle_read.begin(CLK_tip,CLK_handle);
  handle_read.set_scale(55);// change to calibration factor

  pinMode(tare_1, INPUT);
  pinMode(tare_2, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  mode_previous = 0;
  interval = 25;
  max_load_prev = 0;
  interval_prev = 0;
  interval_sum = 0;
  while (digitalRead(start1) == 0){
    if (digitalRead(mode1) == 1){
      mode = 1;
    }
    if (digitalRead(mode2) == 1){
      mode = 2;
    }
    if (digitalRead(mode3) == 1){
      mode = 3;
    }
    if (mode_previous != mode){
      Display_mode_set_up(mode);
      mode_previous = mode;
    }
    if (digitalRead(increment) == 1){
      delay(500);
      if (interval >= 1000){
         interval = 25;
      }
      else if (interval < 1000){
        interval = interval + 25;
      }
    }
    max_load = max_load_reading();
    if (max_load_prev != max_load or interval_prev != interval){
      Display_value_set_up(max_load, interval, mode);
      max_load_prev = max_load;
      interval_prev = interval;
    }
    Display_output(handle_weight, tip_weight);
    tare_one();
  }
  if (mode == 1){
    mode_one(max_load, tip_weight);
  }
  else if (mode == 2){
    mode_two(max_load, interval, handle_weight, tip_weight);
  }
  else if (mode ==3){
    mode_three(max_load, interval, handle_weight, tip_weight);
  }
  Pause_stop();  
}



//---SUB ROUTINES---


void mode_one(int max_load, int tip_weight){
  tip_weight = read_tip();
  while (tip_weight < max_load){
    tip_weight = read_tip();
    turn_motor();
    Pause_stop();
    Display_output(handle_weight, tip_weight);
  }
}

void mode_two(int max_load, int interval, int handle_weight, int tip_weight){
  int aim_value;
  aim_value = 0 + interval;
  tip_weight = read_tip;
  while (tip_weight < max_load){
    while (tip_weight < aim_value){
      turn_motor();
      Pause_stop();
      Display_output(handle_weight, tip_weight);
      tip_weight = read_tip();
    }
    save_to_array();    // <-- Global variable removed
    aim_value = aim_value + interval;
    while (digitalRead(start2) == 0){
      delay(1);
    }
  }
  send_values_to_pc();
}

void mode_three(int max_load, int interval, int handle_weight, int tip_weight){
  int aim_value;
  aim_value = 0 + interval;
  tip_weight = read_tip();
  while (tip_weight < max_load){
    while (tip_weight < aim_value){
      turn_motor();
      Pause_stop();
      Display_output(handle_weight, tip_weight);
      tip_weight = read_tip();
    }
    save_to_array();    // <-- Global variable removed
    aim_value = aim_value + interval;
    delay(10000);
  } 
  send_values_to_pc(); 
}


void tare_one(){
  if (tare_1 == HIGH) {
    tip_read.tare();
  }
  if (tare_2 == HIGH) {
    handle_read.tare();
  }
}

void turn_motor(){
  digitalWrite(dirPin, HIGH);
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(500);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(500);
}

int read_tip(){
  int weight;
  weight = tip_read.get_units();
  return weight;
}
int read_handle(){
  int weight;
  weight = handle_read.get_units();
  return weight;
}

void save_to_array(){    // <-- Global variable removed
  data_storage_array[interval_sum][0] = interval_sum;
  data_storage_array[interval_sum][1] = tip_read.get_units();
  data_storage_array[interval_sum][2] = handle_read.get_units();
  if (interval_sum >= 249){   // <-- Added this if statement as there was no way to cap this number
      interval_sum = 0;
  } 
  else{
      interval_sum = interval_sum + 1;    
  }
}

void send_values_to_pc(){
  
}


int max_load_reading(){
  int KG_value;
  int g_value;
  int load_max;
  KG_value = analogRead(potentiometer1);
  g_value = analogRead(potentiometer2);
  KG_value = KG_value * 2;
  KG_value = KG_value/ 100;
  KG_value = round(KG_value);
  KG_value = KG_value* 100;
  g_value = g_value/ 10;
  g_value = round(g_value);
  if (g_value >= 100){
    g_value = 99;
  }
  load_max = KG_value + g_value;
  return load_max;
}

void Pause_stop(){
  if (digitalRead(stop1) == 1){
    delay(1000);
    while (digitalRead(start1) == 0){
      if (digitalRead(stop1) == 1){
        motor_unwind();
      }
      else{
        delay(100);
        lcd.setCursor(0, 2);
        lcd.print("Program Paused");
      }      
    }
    lcd.setCursor(0, 2);
    lcd.print("                    ");
  }  
}

void motor_unwind(){
  tip_weight = read_tip();
  while (tip_weight < 100){
    digitalWrite(dirPin, LOW);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  }
  int count = 100;
  while (count > 0){
    digitalWrite(dirPin, LOW);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
    count = count -1;
  }
}

void Display_mode_set_up(int mode){
  lcd.setCursor(0, 0);
  if (mode == 1){
    lcd.print("Mode: Max           ");
    lcd.setCursor(0, 1);
    lcd.print("MLoad:");
    lcd.setCursor(12, 1);
    lcd.print("inti:---");
  }
  if (mode == 2){
    lcd.print("Mode: Incrimental    ");
    lcd.setCursor(0, 1);
    lcd.print("MLoad:");
    lcd.setCursor(12, 1);
    lcd.print("inti:   ");    
  }
  if (mode == 3){
    lcd.print("Mode: Automatic      ");
    lcd.setCursor(0, 1);
    lcd.print("MLoad:");
    lcd.setCursor(12, 1);
    lcd.print("inti:   ");   
  }
  lcd.setCursor(0, 3);
  lcd.print("Tip:");
  lcd.setCursor(9, 3);
  lcd.print("Handle:");
}

void Display_value_set_up(int max_load, int interval, int mode){
  if (mode == 1){
    lcd.setCursor(6, 1);
    lcd.print("      ");
    lcd.setCursor(6, 1);
    lcd.print(max_load);
  }
  if (mode == 2 or mode == 3){
    
    lcd.setCursor(6, 1);
    lcd.print("      ");
    lcd.setCursor(6, 1);
    lcd.print(max_load);
    
    lcd.setCursor(17, 1);
    lcd.print("   ");
    lcd.setCursor(17, 1);
    lcd.print(interval);
  }
}

void Display_output(int handle_weight, int tip_weight){
  tip_weight = read_tip();
  handle_weight = read_handle();
  lcd.setCursor(4, 3);
  lcd.print(tip_weight);
  lcd.setCursor(16, 3);
  lcd.print(handle_weight);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.