Defining values in Void Setup

Hi,
I'm new to this forum - and relatively new to Arduino boards and programming. I wasn't sure what category this topic should come under so if its in the wrong one then I apologise.

Basically, I am creating a program which requires a value based on an external sensor to be recorded so that it can be used at a future point. I will include a copy of the program at the end of this message.

What I want to know is this, is there any way of storing a value in the Void Setup () { section?

I did a small amount of this a couple of years ago, however, I haven't done anything between then and now, nor have I done any programming prior to that occasion. As such I am still very much a beginner and have a lot to learn. Prior to this I have trawled the forum, the internet and the example programs, however, I cannot find the information I am looking for. Thus, I come to this forum as a last resort to see if anyone can help me.

The program is linked.
The values I am attempting to define are SensorLight, SensorDark and gateSenDark. The pins A0 and A1 are connected to two potential dividers (one each) formed of a resistor and LDR. The Button (A2) value will be connected to A2 via button (currently being represented by a wire from the 5V pin to pin A2.

Mitchell

Brockhampton_gate_program_lcd.ino (2.23 KB)

Stella123:
is there any way of storing a value in the Void Setup () { section?

Set up runs once. Yes you can read a sensor and store a value in your setup code.

...but it is then unavailable to any other function, like loop(), unless you provide a global pointer to it.

In which case, you may as well just have made the variable global.

Please remember to use code tags when posting code.

As already said, the easiest way for you to do that is declaring a global variable

Variable scope

Ok, thanks for the responses.
You say I need to add a global pointer. In laymans terms what is this?

You must understand, I have only got into computers, electronics and microcomputers in the last few months, thus, most of these terms don't make sense to me. Nevertheless, I will learn.

The way I understand it, I could also make the values global by creating them outside the setup or loop codes. However, I need the lcd display which, I also understand, needs to be inside such code. Is this correct or am I talking nonsense?

Cheers Mitchell

P.S. Code tags?

Forget that last line. Think I've found the code tags now.

Mitchell

Why do you think would want to define a variable inside setup()?

Do you understand what scope means?

I have copied the code using code tags to make things easier.

//Brockhampton brewery gate program

int Sensor = A0; // Assigns analog pin 0 to sensor
int gateSen = A1; // Assigns analog pin 1 to gateSen
int Button = A2; //Assigns analog pin 2 to Button
#include <Servo.h>
Servo gate;

#include <LiquidCrystal.h> //Includes LiquidCrystal Library
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); //(rs, enable, d4 d5 d6 d7)

int SensorLight = 0; //stores the value of SensorLight pin
int SensorDark = 0; //stores the value of SensorDark pin
int gateSenDark = 0; //stores the value of gateSenDark pin

void setup() {
  pinMode(Sensor, INPUT); //Sets Sensor pin as an input
  pinMode(gateSen, INPUT); //Sets gateSen pin as an input
  pinMode(Button, INPUT); //Sets the Button pin as an input
  gate.attach(10); //Sets servo control to pin 10
  lcd.begin(16, 2);
  for(analogRead(Button); analogRead(Button) <= 500; lcd.clear(), lcd.print("Record 1st"), lcd.setCursor(0,2), lcd.print("Sensor Value"), delay(100)){
  }
  SensorLight = analogRead(Sensor);
  lcd.clear();
  lcd.print("Recorded 1st");
  delay(5000);
  lcd.clear();
  for(analogRead(Button); analogRead(Button) <= 500; lcd.clear(), lcd.print("Record 2nd"), lcd.setCursor(0,2), lcd.print("Sensor Value"), delay(100)){
  }
  SensorDark == analogRead(Sensor);
  lcd.clear();
  lcd.print("Recorded 2nd");
  delay(5000);
  lcd.clear();
  for(analogRead(Button); analogRead(Button) <= 500; lcd.clear(), lcd.print("Record 3rd"), lcd.setCursor(0,2), lcd.print("Sensor Value"), delay(100)){
  }
  gateSenDark == analogRead(gateSen);
  lcd.clear();
  lcd.print("Recorded 3rd");
  delay(5000);
  lcd.clear();
  lcd.print("Recording");
  lcd.setCursor(0,2);
  lcd.print("Completed");
  gate.write(0);
  delay(10000);
}

void loop() {
  lcd.clear();
  lcd.print("Operating");

  for(analogRead(Sensor); analogRead(Sensor) <= (SensorLight+50); gate.write(0)){
  }
  gate.write(90);
  (delay(6000)); //Waits 6 seconds before continuing with program
  
    for(analogRead(Sensor) && analogRead(gateSen); analogRead(Sensor) >= (SensorDark-50) || analogRead(gateSen) >= (gateSenDark-50); gate.write(90)){
    }
      gate.write(0);
  delay(10000); //Waits 10 seconds before restarting program
}

Mitchell

  for(analogRead(Button); analogRead(Button) <= 500; lcd.clear(), lcd.print("Record 2nd"), lcd.setCursor(0,2), lcd.print("Sensor Value"), delay(100)){
  }
  SensorDark == analogRead(Sensor);

I'm not sure what that for loop is doing (it may be OK, but it looks weird), and I'm pretty sure you didn't want to use == .

AWOL:
Why do you think would want to define a variable inside setup()?

Do you understand what scope means?

Not in the slightest.

AWOL:
I'm not sure what that for loop is doing (it may be OK, but it looks weird), and I'm pretty sure you didn't want to use == .

The idea behind the loop is that the code keeps searching for a change in the state of the Button pin whilst external factors are changed. Also, I wanted a visual output via the LCD. The loop is currently working, however, the issue is that it isn't recording a value (so you could be right about the '==').
I try to achieve what I wan by using methods and code that have worked for me before. Up until this point that has worked for me. The rest of the code I know works, however, this may not be the best way of recording the value, hence why I have appealed to the forum.

Mitchell

for (analogRead(Button); analogRead(Button) <= 500; lcd.clear(), lcd.print("Record 1st"), lcd.setCursor(0, 2), lcd.print("Sensor Value"), delay(100)) {
  }

seems to roughly be the equivalent to:

  while (analogRead(button) <= 500) {
    lcd.clear();
    lcd.print("Record 1st");
    lcd.setCursor(0, 2);
    lcd.print("Sensor Value");
    delay(100);
  }

But would more conventionally be written as

  lcd.clear();
  lcd.print("Record 1st");
  lcd.setCursor(0, 2);
  lcd.print("Sensor Value");
  while (analogRead(button) <= 500) {  //wait for button to be pressed??
    delay (100);
  }

numRepeat = ++numRepeat; Don't do that.

A simple numRepeat++; is sufficient.

Hi,
Out of interest whats the difference?
It appears to work anyway.

Mitchell

 numRepeat = ++numRepeat;

Is the same as...

numRepeat++;
numRepeat = numRepeat;

You shouldn't use it, because sooner or later you'll write something like "x = x++;", the result of which is specifically undefined.

The result of "x = ++x;" used to be similarly undefined, but I believe is now defined, but only since C++11.