1

Its a bit of a long piece of code so thank anyone for your help, I'm having several problems with it. at one point in time when I was testing it when I called sub routines they worked fine. but now when I call them it gives me a "was not declared in this scope".
Ive checked variable names to make sure they match, and I have not got any other clues what Ive done.

My second large error is when trying to read two load cell amplifiers (the code ive used to do this is copied and I dont realy understand it.

I'm entirely self tought so please forgive any stupid errors.

#include <HX711.h>

#include <HX711_ADC.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 mode1 = 53;
int mode2 = 51;
int mode3 = 49;
int start1 = 39;
int start2 = 37;
int increment = 47;
float potentiometer1 = A0;
float potentiometer2 = A1;


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

//tare
int tare_1_pin = 45;
int tare_2_pin = 43;
boolean tare_1 = LOW;
boolean tare_2 = LOW;
boolean loadcell_1_rdy = false;
boolean loadcell_2_rdy = false;
boolean _tare = true;
int stabilising_time = 2000;




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

  //saving to array//
  float data_storage_array[1500][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);




  //load cell 2
  Serial.begin(9600);
  int LCA_DOUT_2_pin = 6;
  int LCA_SCK_2_pin = 7;
  float CalibrationValue2 = 733.0;
  float LCA_OutValue2 = 0;
  boolean NewDataRdy_2 = false;

  HX711_ADC LoadCell_2(LCA_DOUT_2_pin, LCA_SCK_2_pin);

  LoadCell_2.begin();
  LoadCell_2.setCalFactor(CalibrationValue2);

  //load cell 1
  int LCA_DOUT_1_pin = 4;
  int LCA_SCK_1_pin = 5;
  float CalibrationValue1 = 696.0;
  float LCA_OutValue1 = 0;
  boolean NewDataRdy_1 = false;

  HX711_ADC LoadCell_1(LCA_DOUT_1_pin, LCA_SCK_1_pin);

  LoadCell_1.begin();
  LoadCell_1.setCalFactor(CalibrationValue1);



}

void loop() {
  // put your main code here, to run repeatedly:
  mode_previous = 0;
  interval = 25;
  max_load_prev = 0;
  interval_prev = 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(mode);
    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(mode);
    aim_value = aim_value + interval;
    delay(10000);
  } 
  send_values_to_pc(); 
}


void tare_one(){
  if (tare_1 == HIGH) {
    if (!loadcell_1_rdy) loadcell_1_rdy = LoadCell_1.startMultiple(stabilising_time, _tare);
    LoadCell_1.setCalFactor(CalibrationValue1);
  }
  if (tare_2 == HIGH) {
    if (!loadcell_2_rdy) loadcell_2_rdy = LoadCell_2.startMultiple(stabilising_time, _tare);
    LoadCell_2.setCalFactor(CalibrationValue2);
  }
}

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

int read_tip(){
/*
  if (LoadCell_2.update()) NewDataRdy_2 = true;
  if (NewDataRdy_2 == true) {
    LCA_OutValue2 = LoadCell_2.getData();
    NewDataRdy_2 = false;
  }
  ScreenOut_2 = (round(10000*LCA_OutValue2))/10000;
  return ScreenOut_2;
}
*/
int read_handle(){
/*
  if (LoadCell_1.update()) NewDataRdy_1 = true;
  if (NewDataRdy_1 == true){
    LCA_OutValue1 = LoadCell_1.getData();
    NewDataRdy_1 = false;
  }
  ScreenOut_1 = (round(10000*LCA_OutValue1))/10000;
  return ScreenOut_1;
}
*/
void save_to_array(int mode){
  int interval_sum = 0;
  int max_interval = 0;
  if (mode == 1) {
    for (interval_sum = 0; interval_sum < 1501; interval_sum++) {
      data_storage_array[interval_sum][1] = LCA_OutValue1;
      data_storage_array[interval_sum][2] = LCA_OutValue2;
      data_storage_array[interval_sum][3] = interval_sum;
      delay(10000);
    }
  }
  if (mode != 1) {
    for (interval_sum = 0; interval_sum < max_interval; ) {
      data_storage_array[interval_sum][1] = LCA_OutValue1;
      data_storage_array[interval_sum][2] = LCA_OutValue2;
      data_storage_array[interval_sum][3] = interval_sum;
    }
  }
}

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 (tp_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);
}
  • 2
    You have commented out lots of bits of your code and ended up with mismatched brackets. – Majenko Sep 6 at 16:38
  • @Majenko, indeed. Specifically, almost all of read_tip() is commented out, except for the opening bracket. The same goes for read_handle(). So you end op with mismatched brackets. Everything after those two functions will not compile correctly. – StarCat Sep 6 at 16:50
  • @Majenko, thanks thats really helped!!:) – Jacob Huggins Sep 6 at 16:54

Browse other questions tagged or ask your own question.