Help with REC Button please

I am trying to have an Arduino control a video camera. I need the Arduino to turn on a camera and ensure that it stays on, once it is on I need the Arduino to make sure that it continously records. I have the camera turning on and if I manually shut it off the Arduino will automatically turn it back on, so that part is working; I am however having trouble trying to make the Arduino know when it’s recording. I have a LDR connected to a blinking red light on the front of the camera in hopes that the arduino will know if it’s recording or not. I have no idea hot to use the “Millis” command and I am hoping to use this camera by this weekend so I was trying to improvise. I was trying to have the Arduino take a reading from the LED to see if the camera is recording or not. If the LED is off/or below a threshold I set I was trying to have it take another reading in a short bit in hopes that I’m just not taking the reading while the LED is on it’s off position while it is blinking. The problem I’m having is that it doesnt seem to be taking a new reading after the first reading. I am trying to make it take 5 readings to alleviate it taking the reading when the LED is off. This is the code I have been using. I am wondering if someone can please tell me what I am doing wrong here.

// These constants won't change:
const int analogPin = A0;    // pin that the camera PWR sensor is attached to
const int analogPin1 = A1;    // pin that the LDR is connected to
const int ledPin = 12;       // pin that the camera PWR button is attached to
const int ledPin1 = 8;        // pin that the camera REC button is attached to
const int threshold = 475;   // an arbitrary threshold level for the Camera PWR
const int threshold1 = 25;  // an arbitrary threshold level for the REC LED

void setup() {
   pinMode(ledPin, OUTPUT);  // initialize the Camera PWR button as an output:
   pinMode(ledPin1, OUTPUT);  // initialize the Camera REC button as an output:
  
  Serial.begin(9600);        // initialize serial communications:
}

void loop() {
  // read the value of the camera PWR:
  int analogValue = analogRead(analogPin);
  // read the value of the LDR LED:
  int analogValue1 = analogRead(analogPin1);
  

  // if the analog value is too low, "Press" the PWR button:
  if (analogValue < threshold) {
    Serial.println("Pressing PWR in 2 seconds; then delay for 5 seconds");
    delay(2000);
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(5000);
  } 
  else {
    digitalWrite(ledPin,LOW); 
  }


// if the analog value is too low, "Press" the REC button:
  if (analogValue1 < threshold1) {
    Serial.println("LED detection failed 1st check");
    Serial.println(analogValue1, DEC);
    delay(1700);
    
    analogRead(analogPin1);
    if (analogValue1 < threshold1)
    Serial.println("LED detection failed 2nd check");
    Serial.println(analogValue1, DEC);
    delay(1700);
    
    analogRead(analogPin1);
    if (analogValue1 < threshold1)
    Serial.println("LED detection failed 3rd check");
    Serial.println(analogValue1, DEC);
    delay(1700);
    
    analogRead(analogPin1);
    if (analogValue1 < threshold1)
    Serial.println(analogValue1, DEC);
    Serial.println("LED detection failed 4th check");
    delay(1700);
    
    analogRead(analogPin1);
    if (analogValue1 < threshold1)
    Serial.println(analogValue1, DEC);
    Serial.println("LED detection failed 5th check.... Pressing REC button in 2 seconds");
    delay(2000);
    
    
    digitalWrite(ledPin1, HIGH);
    delay(500);
    digitalWrite(ledPin1, LOW);
    delay(500);
  } 
  else {
    digitalWrite(ledPin1,LOW); 
  }

  // print the analog value:
  Serial.println(analogValue, DEC);
  Serial.println(analogValue1, DEC);

}
const int analogPin = A0;    // pin that the camera PWR sensor is attached to

So, why isn’t the variable called something meaningful, like powerPin?

const int analogPin1 = A1; // pin that the LDR is connected to

So, why isn’t the variable called something meaningful, like ldrPin?

const int ledPin = 12;       // pin that the camera PWR button is attached to
const int ledPin1 = 8;        // pin that the camera REC button is attached to
const int threshold = 475;   // an arbitrary threshold level for the Camera PWR
const int threshold1 = 25;  // an arbitrary threshold level for the REC LED

So, why not use meaningful names?

  // read the value of the camera PWR:
  int analogValue = analogRead(analogPin);
  // read the value of the LDR LED:
  int analogValue1 = analogRead(analogPin1);

At the risk of repeating myself, these are lousy variable names. Give them meaningful names.

    analogRead(analogPin1);
    if (analogValue1 < threshold1)

This reading is useless. You don’t store the result in the variable, so the value of the variable hasn’t changed.

thank you for the reply,

the answer to all of ur questions is "because i don't know what I'm doing with this". how would I go about storing the value so I could have something to compare it to?

how would I go about storing the value so I could have something to compare it to?

Just like you did the first 2 analgoRead()s.

Thank you for the response and please excuse my ignorance i am truly stumbling through this. i think i did something right but it only seemed to work once; meaning my 1st and 2nd reading are now different but reading 3,4 and 5 are the same as reading 2. I tried to do what I believe you told me to but it only seemded to work once. I tried to insert it in before taking readings 3,4 and 5 but it would give me an error message. here is what i did which did seem to bring me 1 step closer.

// if the analog value is too low, "Press" the REC button:
  if (analogValue1 < threshold1) {
    Serial.println("LED detection failed 1st check");
    Serial.println(analogValue1, DEC);
    delay(1700);
    
    int analogValue1 = analogRead(analogPin1);          <--------------------this is where i could get it to work to take 
    analogRead(analogPin1);                                                                 to take a new reading
    if (analogValue1 < threshold1)
    Serial.println("LED detection failed 2nd check");
    Serial.println(analogValue1, DEC);
    delay(1700);
    
                                                                                <----------------- I tried the same thing here but got an error 
    analogRead(analogPin1);                                                               msg
    if (analogValue1 < threshold1)
    Serial.println("LED detection failed 3rd check");
    Serial.println(analogValue1, DEC);
    delay(1700);
    
    analogRead(analogPin1);                                        <-------------------tried again here
    if (analogValue1 < threshold1)
    Serial.println(analogValue1, DEC);
    Serial.println("LED detection failed 4th check");
    delay(1700);
    
    analogRead(analogPin1);                                         <------------------- and tried again here
    if (analogValue1 < threshold1)
    Serial.println(analogValue1, DEC);
    Serial.println("LED detection failed 5th check.... Pressing REC button in 2 seconds");
    delay(2000);
    
    
    digitalWrite(ledPin1, HIGH);
    delay(500);
    digitalWrite(ledPin1, LOW);
    delay(500);
  } 
  else {
    digitalWrite(ledPin1,LOW); 
  }

  // print the analog value:
  Serial.println(analogValue, DEC);
  Serial.println(analogValue1, DEC);

}

thank you for your help

Hi,

I don’t get some of the lines in your code, for example:

int analogValue1 = analogRead(analogPin1);
    analogRead(analogPin1);   
    if (analogValue1 < threshold1)

Why are you doing the analogRead a second Time without saving the results to a variable? Line 2 makes no sense to me.

Regarding your question: This should work for the second part (if it doesn’t then you should provide the error message):

analogValue1 = analogRead(analogPin1); 
if (analogValue1 < threshold1)
    Serial.println("LED detection failed 3rd check");

You do not need to re-declare the variable again to the “int” part before the analogValue1 is missing (actually I am unsure if this would even work).

Hope this helps,
Christoph

Try this:

    Serial.println("LED detection failed 1st check");
    Serial.println(analogValue1, DEC);
    delay(1700);
    
    analogValue1 = analogRead(analogPin1); // NOTE that the initial 'int' is gone
    if (analogValue1 < threshold1)
    Serial.println("LED detection failed 2nd check");
    Serial.println(analogValue1, DEC);
    delay(1700);

Thank you Christoph, I have taken those lines out that you asked about. I think I thought I was telling the arduino to take a reading at that time. I am completely new and do not know how to program, I have only gotten this far by copying and pasting different parts of tutorials that I thought I needed, so I am very grateful for the help. This is now what the code looks like

// These constants won't change:
const int analogPin = A0;    // pin that the camera PWR sensor is attached to
const int analogPin1 = A1;    // pin that the LDR is connected to
const int ledPin = 12;       // pin that the camera PWR button is attached to
const int ledPin1 = 8;        // pin that the camera REC button is attached to
const int threshold = 475;   // an arbitrary threshold level for the Camera PWR
const int threshold1 = 25;  // an arbitrary threshold level for the REC LED

void setup() {
   pinMode(ledPin, OUTPUT);  // initialize the Camera PWR button as an output:
   pinMode(ledPin1, OUTPUT);  // initialize the Camera REC button as an output:
  
  Serial.begin(9600);        // initialize serial communications:
}

void loop() {
  // read the value of the camera PWR:
  int analogValue = analogRead(analogPin);
  // read the value of the LDR LED:
  int analogValue1 = analogRead(analogPin1);
  

  // if the analog value is too low, "Press" the PWR button:
  if (analogValue < threshold) {
    Serial.println("Pressing PWR in 2 seconds; then delay for 5 seconds");
    delay(2000);
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(5000);
  } 
  else {
    digitalWrite(ledPin,LOW); 
  }


// if the analog value is too low, "Press" the REC button:
  if (analogValue1 < threshold1) {
    Serial.println("LED detection failed 1st check");
    Serial.println(analogValue1, DEC);
    delay(1700);
    
    int analogValue1 = analogRead(analogPin1);
        if (analogValue1 < threshold1)
    Serial.println("LED detection failed 2nd check");
    Serial.println(analogValue1, DEC);
    delay(1700);
    

    analogValue1 = analogRead(analogPin1); 
    if (analogValue1 < threshold1)
    Serial.println("LED detection failed 3rd check");
    Serial.println(analogValue1, DEC);
    delay(1700);
    
    
    analogValue1 = analogRead(analogPin1);
    if (analogValue1 < threshold1)
    Serial.println(analogValue1, DEC);
    Serial.println("LED detection failed 4th check");
    delay(1700);
    
    
    analogValue1 = analogRead(analogPin1);
    if (analogValue1 < threshold1)
    Serial.println(analogValue1, DEC);
    Serial.println("LED detection failed 5th check.... Pressing REC button in 2 seconds");
    delay(2000);
    
    
    digitalWrite(ledPin1, HIGH);
    delay(500);
    digitalWrite(ledPin1, LOW);
    delay(500);
  } 
  else {
    digitalWrite(ledPin1,LOW); 
  }

  // print the analog value:
  Serial.println(analogValue, DEC);
  Serial.println(analogValue1, DEC);

}

What I am trying to do is if the Arduino checks the REC LED 5 consecutive times and finds it to not be lit up I am trying to make it hit the “REC BUTTON”, but I am wondering how could I make it cancel this check if on the 3rd time it sees that it is lit up. I believe it would be some sort of command similiar to this "if (analogValue1 > threshold1) I would like to somehow tell it to restart back at the beginning of the program. Is there some sort of way I could make it say “if (analogValue1 > threshold1) Goto line 1” or something similiar to that?