How to hold variables in a program

Hello, I am currently trying to get my servo to react based on light hitting a photoresistor. I can get my servo to react but I’m having trouble getting it to do a series of actions. I need the servo to perform an action when light is turned on, then maintain that position as the light is removed until light it is exposed once again to brighter light. I can manage to get the servo to perform an action when light is turned on and then reverse itself when light is removed but I can’t get it to perform a different function the second time the light is removed. I’ve tried creating variables that increase in each step of the function using if statements but it seems like each time the light is taken away the variables revert back and don’t continue to build making them unusable to perform successive operations. I’ve written probably a dozen codes but here is something, hopefully my logic can be followed and someone can give me a little direction.

#include <Servo.h>

/*
Into Robotics
*/

int servoPin = 42;

Servo servo;

int servoAngle = 0; // servo position in degrees

//Constants
const int pResistor = A0; // Photoresistor at Arduino analog pin A0

//Variables
int value; // Store value from photoresistor (0-1023)
int J;
int T;
int Q;
int S;
void setup(){
pinMode(pResistor, INPUT);// Set pResistor - A0 pin as an input (optional)
Serial.begin(9600);
Serial.begin(9600);
servo.attach(servoPin);
J=0;
value=0;
}

void loop(){
value = analogRead(pResistor);
Serial.print(value);
Serial.print("\t");
delay(500);
Serial.print(J);
Serial.print("\t");
Serial.print(Q);
Serial.print("\t");
Serial.print(T);
Serial.print("\t");
if (value>950){
J=1;
}
else {
J=0;
}
if (J==1);{
T=1;
}
if (T==1){
servo.write(180);
}
if (T==1 && J==0);{
Q=1;
}
if (Q==1 && J==1);{
servo.write(90);
}
}

JTQ are poor variable names. The compiler removes the names during compilation so they don't waste space on the limited memory of the Arduino. So make the names into short descriptions of what they contain.

Names like firstLightDetected or destinationAchieved.

if (J==1);{

oops

if (T==1 && J==0);{
...
if (Q==1 && J==1);{

and again, oops

Please remember to use code tags when posting code

Thanks for getting back to me, I don’t think the variable names is the issue I was having. I’ve messed around with it a bit more and think part of my problem is it wasn’t changing my value for my variable value between the loops, but I’m not sure that is the problem either. The first operation for the servo is functioning properly but I can’t seem to get the second function to operate as it should in that it will operate after the first operation after the delay regardless of light. The program also will not function without the first delay, as in I get no response out of the servo. The first delay is fine and won’t cause issues, but I’m still perplexed as to why the second operation happens regardless. My value for secondlight becomes one after the first iteration even if the sensor is blocked and the requirements for getting there are never satisfied. Here is my updated code if anyone can help me with it.

/* Use a photoresistor (or photocell) to turn on an LED in the dark
   More info and circuit schematic: http://www.ardumotive.com/how-to-use-a-photoresistor-en.html
   Dev: Michalis Vasilakis // Date: 8/6/2015 // www.ardumotive.com */
   
#include <Servo.h>

/*
Into Robotics
*/
 
 
int servoPin = 42;
 
Servo servo;  
 
int servoAngle = 0;   // servo position in degrees
 
//Constants
const int pResistor = A0; // Photoresistor at Arduino analog pin A0


//Variables
int value;   // Store value from photoresistor (0-1023)
int firstlight; 
int secondlight;
int inbetween;
int S;
void setup(){
 pinMode(pResistor, INPUT);// Set pResistor - A0 pin as an input (optional)
 Serial.begin(9600);
 Serial.begin(9600);  
 servo.attach(servoPin);
 firstlight=0;
 value=0;
}

void loop(){
  value = analogRead(pResistor);
  Serial.print(value);
  Serial.print("\t");
  delay(500);
  Serial.print(firstlight);
  Serial.print("\t");
  Serial.print(inbetween);
  Serial.print("\t");
  Serial.print(secondlight);
  Serial.print("\t");
if (value>950){
  firstlight=1;  
}
else {
  firstlight=0;
  inbetween=0;
  secondlight=0;
}
if (firstlight==1){
  servo.write(180);
  delay(1000);
  inbetween=1;
  secondlight=0;
}
value = analogRead(pResistor);
if (value>950){
  firstlight=1;  
}
else {
  firstlight=0;
}
if ((inbetween==1) && (firstlight==0));{
  secondlight=1;
}
value = analogRead(pResistor);
if (value>950){
  firstlight=1;  
}
else {
  firstlight=0;
}
if ((secondlight==1) && (firstlight==1));{
  servo.write(90);
  
}
}
if ((inbetween==1) && (firstlight==0));{

Still oops.

AWOL:

if (J==1);{

oops

if (T==1 && J==0);{

...
if (Q==1 && J==1);{



and again, oops


Please remember to use code tags when posting code

I figured out the code tag, sorry about that. Could you let me know why you said oops, I'm somewhat familiar to coding but I'm new to using Arduino.

All right, all right - do you know what a semicolon does?

Paul__B:
All right, all right - do you know what a semicolon does?

Clearly no, getting rid of them seemed to help though thank you.

There is no need to use semicolons in if statements or other conditional things. I have never tried it, I would have thought the compiler would through an error.

OK, the semicolon closes the statement so it has no exit control status that affects the following.

Generally if you fail to close a statement with a semicolon it becomes part of the next and fails to make sense so the compiler baulks, but if you choose not to make use of the conditional it just says "whatever"!

IIRC, you must not put a semicolon in front of an else either.

Thank you for the explanation, I’m more familiar with Matlab which uses it just to keep it from being displayed in the outcome screen.