Pages: [1]   Go Down
Author Topic: Problem with a timed threshold..... I think?  (Read 591 times)
0 Members and 1 Guest are viewing this topic.
Boston, Ma.
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, I am trying to have an arduino uno control 2 cameras and ensure that they constantly stay on and constanlty record. I have wired the arduino to internal parts of the camera such as the rec button, the power button, an internal power line and i have setup a LDR to the blinking LED on the camera that flashes when it records. I have had a lot of help with the software and I believe it was working fine; but it has sat in my basement for a few years due to having kids and work, but I am trying to get it back up and running now so I can attach these to a weather balloon this Sunday. I have powered it up but it seems like it is only taking the reading of the blinking LED on the camera for a split second but I would like it to monitor the blinking LED for a few seconds (perhaps anywhere from 5-10) and if the LED does not come on I would then like it to send a signal to the REC Button to record; but like i said it seems to just be taking 1 reading and if it happens to take that reading while the LED is blinking and in the off state it hits the REC button. any help would be GREATLY APPRECIATED. Like I said I don't really know what I'm doing here so if i have left out any information that you may need just let me know. Also I can provide pictures or video or anything you may need or we could even video conference if that makes it easier. Please let me know.

 
// These constants won't change:
const int PWRsensor2 = A2;     // was "analogpin" pin - camera PWR sensor is attached to
const int LDR2 = A3;           // was "analogpin1" pin - LDR
const int PWRled2 = 13;        // was "ledpin" pin - camera PWR button is attached to
const int RECled2 = 9;         // was "ledpin1" pin - camera REC button is attached to
const int PWRsensor1 = A0;     // was "analogpin" pin - camera PWR sensor is attached to
const int LDR1 = A1;           // was "analogpin1" pin - LDR
const int PWRled1 = 12;        // was "ledpin" pin - camera PWR button is attached to
const int RECled1 = 8;         // was "ledpin1" pin - camera REC button is attached to
const int threshold = 400;    // threshold for the Camera PWR
const int ledIsOnLevel = 20;  // threshold for the REC LED
const int blinkTime = 5000;

void startRecording1 ()
{

  Serial.println("                                                 Pressing REC BUTTON on Camera 1");
  digitalWrite(RECled1, HIGH);
  delay(500);
  digitalWrite(RECled1, LOW);
  delay(500);
}
void startRecording2 ()
{

  Serial.println("                                                 Pressing REC BUTTON on Camera 2");
  digitalWrite(RECled2, HIGH);
  delay(500);
  digitalWrite(RECled2, LOW);
  delay(500);
}

//---------------------------

void setup()
{
  pinMode(PWRled1, OUTPUT);   // init Camera 1 PWR button = output:
  pinMode(RECled1, OUTPUT);   // init Camera 1 REC button = output:
  pinMode(PWRled2, OUTPUT);   // init Camera 2 PWR button = output:
  pinMode(RECled2, OUTPUT);   // init Camera REC 2 button = output:
  Serial.begin(9600);         // init serial comms:
}

void loop()

  int PWRValue1 = analogRead(PWRsensor1);  // read camera PWR
  int LDRValue1 = analogRead(LDR1);        // read LDR LED
  int PWRValue2 = analogRead(PWRsensor2);  // read camera PWR
  int LDRValue2 = analogRead(LDR2);        // read LDR LED
  // if the analog value is too low, "Press" the PWR button:

  Serial.println("                         Power Reading Camera 1");
  Serial.println(PWRValue1);
  Serial.println("                         Power Reading Camera 2");
  Serial.println(PWRValue2);
  Serial.println("LDR Reading Camera 1");
  Serial.println(LDRValue1, DEC);
  Serial.println("LDR Reading Camera 2");
  Serial.println(LDRValue2, DEC);
  delay(500);

  // if the analog value is too low, "Press" the PWR button:
  if (PWRValue1 < threshold)
  {
    Serial.println("Pressing PWR on Camera 1 in 2 secs; then delay for 5 secs");
    delay(2000);
    digitalWrite(PWRled1, HIGH);
    delay(500);
    digitalWrite(PWRled1, LOW);
    delay(5000);
    Serial.println("Resuming");

  }
  else
  {
    digitalWrite(PWRled1,LOW);
  }


  if (PWRValue2 < threshold)
  {
    Serial.println("Pressing PWR on Camera 2 in 2 secs; then delay for 5 secs");
    delay(2000);
    digitalWrite(PWRled2, HIGH);
    delay(500);
    digitalWrite(PWRled2, LOW);
    delay(5000);
    Serial.println("Resuming");

  }
  else
  {
    digitalWrite(PWRled2,LOW);


  }

  // if the analog value is too low for 3 seconds on Camera 1, "Press" the REC button:
  bool recording1 = false;
  unsigned long blinkStart = millis();
  while(millis() - blinkStart < blinkTime)
  {
    if(analogRead(LDR1) > ledIsOnLevel)

    {
      recording1 = true;
      Serial.println("                                                  Camera 1 is RECORDING");
      break;
    }
  }

  if(!recording1)
  {
    Serial.println("                                                 Camera 1 is  NOT RECORDING");                                  // r_p     
    startRecording1();
  }

 
 
  // if the analog value is too low for 3 seconds on Camera 1, "Press" the REC button:
  bool recording2 = false;

  while(millis() - blinkStart < blinkTime)
  {
    if(analogRead(LDR2) > ledIsOnLevel)

    {
      recording2 = true;
      Serial.println("                                             Camera 2 is RECORDING");
      break;
    }
  }

  if(!recording2)
  {
    Serial.println("                                                 Camera 2 is NOT RECORDING");                                   // r_p     
    startRecording2();     
    // print the analog value:
  }                                 // r_p

  Serial.println("End of Loop");
}

Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Please post your code inside code tags.  I'm not going to bother trawling through it until you have changed it.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8703
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your problem is Camera 2.  You forgot to put "blinkStart = millis();" before the Camera 2 while loop.  blinkStart is still left at the expired value it had form the Camera 1 loop.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Boston, Ma.
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

will do; sorry i wasnt aware of that and please let me know if theres anything else you need to know or anything. THANK YOU.

Code:
// These constants won't change:
const int PWRsensor2 = A2;     // was "analogpin" pin - camera PWR sensor is attached to
const int LDR2 = A3;           // was "analogpin1" pin - LDR
const int PWRled2 = 13;        // was "ledpin" pin - camera PWR button is attached to
const int RECled2 = 9;         // was "ledpin1" pin - camera REC button is attached to
const int PWRsensor1 = A0;     // was "analogpin" pin - camera PWR sensor is attached to
const int LDR1 = A1;           // was "analogpin1" pin - LDR
const int PWRled1 = 12;        // was "ledpin" pin - camera PWR button is attached to
const int RECled1 = 8;         // was "ledpin1" pin - camera REC button is attached to
const int threshold = 400;    // threshold for the Camera PWR
const int ledIsOnLevel = 20;  // threshold for the REC LED
const int blinkTime = 5000;

void startRecording1 ()
{

  Serial.println("                                                 Pressing REC BUTTON on Camera 1");
  digitalWrite(RECled1, HIGH);
  delay(500);
  digitalWrite(RECled1, LOW);
  delay(500);
}
void startRecording2 ()
{

  Serial.println("                                                 Pressing REC BUTTON on Camera 2");
  digitalWrite(RECled2, HIGH);
  delay(500);
  digitalWrite(RECled2, LOW);
  delay(500);
}

//---------------------------

void setup()
{
  pinMode(PWRled1, OUTPUT);   // init Camera 1 PWR button = output:
  pinMode(RECled1, OUTPUT);   // init Camera 1 REC button = output:
  pinMode(PWRled2, OUTPUT);   // init Camera 2 PWR button = output:
  pinMode(RECled2, OUTPUT);   // init Camera REC 2 button = output:
  Serial.begin(9600);         // init serial comms:
}

void loop()

  int PWRValue1 = analogRead(PWRsensor1);  // read camera PWR
  int LDRValue1 = analogRead(LDR1);        // read LDR LED
  int PWRValue2 = analogRead(PWRsensor2);  // read camera PWR
  int LDRValue2 = analogRead(LDR2);        // read LDR LED
  // if the analog value is too low, "Press" the PWR button:

  Serial.println("                         Power Reading Camera 1");
  Serial.println(PWRValue1);
  Serial.println("                         Power Reading Camera 2");
  Serial.println(PWRValue2);
  Serial.println("LDR Reading Camera 1");
  Serial.println(LDRValue1, DEC);
  Serial.println("LDR Reading Camera 2");
  Serial.println(LDRValue2, DEC);
  delay(500);

  // if the analog value is too low, "Press" the PWR button:
  if (PWRValue1 < threshold)
  {
    Serial.println("Pressing PWR on Camera 1 in 2 secs; then delay for 5 secs");
    delay(2000);
    digitalWrite(PWRled1, HIGH);
    delay(500);
    digitalWrite(PWRled1, LOW);
    delay(5000);
    Serial.println("Resuming");

  }
  else
  {
    digitalWrite(PWRled1,LOW);
  }


  if (PWRValue2 < threshold)
  {
    Serial.println("Pressing PWR on Camera 2 in 2 secs; then delay for 5 secs");
    delay(2000);
    digitalWrite(PWRled2, HIGH);
    delay(500);
    digitalWrite(PWRled2, LOW);
    delay(5000);
    Serial.println("Resuming");

  }
  else
  {
    digitalWrite(PWRled2,LOW);


  }

  // if the analog value is too low for 3 seconds on Camera 1, "Press" the REC button:
  bool recording1 = false;
  unsigned long blinkStart = millis();
  while(millis() - blinkStart < blinkTime)
  {
    if(analogRead(LDR1) > ledIsOnLevel)

    {
      recording1 = true;
      Serial.println("                                                  Camera 1 is RECORDING");
      break;
    }
  }

  if(!recording1)
  {
    Serial.println("                                                 Camera 1 is  NOT RECORDING");                                  // r_p     
    startRecording1();
  }





  // if the analog value is too low for 3 seconds on Camera 1, "Press" the REC button:
  bool recording2 = false;

  while(millis() - blinkStart < blinkTime)
  {
    if(analogRead(LDR2) > ledIsOnLevel)

    {
      recording2 = true;
      Serial.println("                                             Camera 2 is RECORDING");
      break;
    }
  }

  if(!recording2)
  {
    Serial.println("                                                 Camera 2 is NOT RECORDING");                                   // r_p     
    startRecording2();     
    // print the analog value:
  }                                 // r_p

  Serial.println("End of Loop");
}



I think this is what you where saying to do, if not please let me know.
Logged

Boston, Ma.
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

John Wasser I AM VERY HAPPY TO SEE YOU ON HERE. we are going this Sunday if you are interested. I got my ham radio license and we are using an APRS tracking device this time.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep, that's much better.  You can follow the indenting so much easier when you have a left hand edge to reference it against.

Yes, as has been mentioned, the problem is camera 2 and not setting the initial time value each time through.

However, the whole way the program is written is kind of bad.  Every operation is blocking, and it can only do the next test once the previous test has completed.

It would be far better to get rid of all those loops and delays, and implement it with a Finite State Machine.  You could, instead of waiting for a blink, record the last time you saw a blink, and if that time is more than 5 seconds ago, then it's not recording.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Boston, Ma.
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

does this look correct now? is this what you where saying to do?

Code:
// These constants won't change:
const int PWRsensor2 = A2;     // was "analogpin" pin - camera PWR sensor is attached to
const int LDR2 = A3;           // was "analogpin1" pin - LDR
const int PWRled2 = 13;        // was "ledpin" pin - camera PWR button is attached to
const int RECled2 = 9;         // was "ledpin1" pin - camera REC button is attached to
const int PWRsensor1 = A0;     // was "analogpin" pin - camera PWR sensor is attached to
const int LDR1 = A1;           // was "analogpin1" pin - LDR
const int PWRled1 = 12;        // was "ledpin" pin - camera PWR button is attached to
const int RECled1 = 8;         // was "ledpin1" pin - camera REC button is attached to
const int threshold = 400;    // threshold for the Camera PWR
const int ledIsOnLevel = 20;  // threshold for the REC LED
const int blinkTime = 5000;

void startRecording1 ()
{

  Serial.println("                                                 Pressing REC BUTTON on Camera 1");
  digitalWrite(RECled1, HIGH);
  delay(500);
  digitalWrite(RECled1, LOW);
  delay(500);
}
void startRecording2 ()
{

  Serial.println("                                                 Pressing REC BUTTON on Camera 2");
  digitalWrite(RECled2, HIGH);
  delay(500);
  digitalWrite(RECled2, LOW);
  delay(500);
}

//---------------------------

void setup()
{
  pinMode(PWRled1, OUTPUT);   // init Camera 1 PWR button = output:
  pinMode(RECled1, OUTPUT);   // init Camera 1 REC button = output:
  pinMode(PWRled2, OUTPUT);   // init Camera 2 PWR button = output:
  pinMode(RECled2, OUTPUT);   // init Camera REC 2 button = output:
  Serial.begin(9600);         // init serial comms:
}

void loop()

  int PWRValue1 = analogRead(PWRsensor1);  // read camera PWR
  int LDRValue1 = analogRead(LDR1);        // read LDR LED
  int PWRValue2 = analogRead(PWRsensor2);  // read camera PWR
  int LDRValue2 = analogRead(LDR2);        // read LDR LED
  // if the analog value is too low, "Press" the PWR button:

  Serial.println("                         Power Reading Camera 1");
  Serial.println(PWRValue1);
  Serial.println("                         Power Reading Camera 2");
  Serial.println(PWRValue2);
  Serial.println("LDR Reading Camera 1");
  Serial.println(LDRValue1, DEC);
  Serial.println("LDR Reading Camera 2");
  Serial.println(LDRValue2, DEC);
  delay(500);

  // if the analog value is too low, "Press" the PWR button:
  if (PWRValue1 < threshold)
  {
    Serial.println("Pressing PWR on Camera 1 in 2 secs; then delay for 5 secs");
    delay(2000);
    digitalWrite(PWRled1, HIGH);
    delay(500);
    digitalWrite(PWRled1, LOW);
    delay(5000);
    Serial.println("Resuming");

  }
  else
  {
    digitalWrite(PWRled1,LOW);
  }


  if (PWRValue2 < threshold)
  {
    Serial.println("Pressing PWR on Camera 2 in 2 secs; then delay for 5 secs");
    delay(2000);
    digitalWrite(PWRled2, HIGH);
    delay(500);
    digitalWrite(PWRled2, LOW);
    delay(5000);
    Serial.println("Resuming");

  }
  else
  {
    digitalWrite(PWRled2,LOW);


  }

  // if the analog value is too low for 3 seconds on Camera 1, "Press" the REC button:
  bool recording1 = false;
  unsigned long blinkStart = millis();
  while(millis() - blinkStart < blinkTime)
  {
    if(analogRead(LDR1) > ledIsOnLevel)

    {
      recording1 = true;
      Serial.println("                                                  Camera 1 is RECORDING");
      break;
    }
  }

  if(!recording1)
  {
    Serial.println("                                                 Camera 1 is  NOT RECORDING");                                  // r_p     
    startRecording1();
  }





  // if the analog value is too low for 3 seconds on Camera 2, "Press" the REC button:
  bool recording2 = false;
  blinkStart = millis();
  while(millis() - blinkStart < blinkTime)
  {
    if(analogRead(LDR2) > ledIsOnLevel)

    {
      recording2 = true;
      Serial.println("                                             Camera 2 is RECORDING");
      break;
    }
  }

  if(!recording2)
  {
    Serial.println("                                                 Camera 2 is NOT RECORDING");                                   // r_p     
    startRecording2();     
    // print the analog value:
  }                                 // r_p

  Serial.println("End of Loop");
}


Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8703
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

does this look correct now? is this what you where saying to do?
Code:
  // if the analog value is too low for 3 seconds on Camera 2, "Press" the REC button:
  bool recording2 = false;
  blinkStart = millis();
  while(millis() - blinkStart < blinkTime)
  {

Yes.  That should work now.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Boston, Ma.
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

John Wasser your obviously a genius, thank you so much everything seems to be working fine now. Like I said we plan on going this Sunday if you are interested?
Logged

Pages: [1]   Go Up
Jump to: