Digital Write stops working

I am new to the Particle Photon but have done some stuff with Arduinos. I am suing my Photon with the Particle Shield. I am also wiring everything to the shield pins.

I am using the Photon to trigger a camera via the camera’s GPIO opt-isolated input lines. If I execute pulses (digitalwrite) that are a couple of seconds apart, all works well. However, if I execute numerous pulses with a short duration apart, the program running on the Photon loses connection to the internet and I get a flashing green. The only way to get the micro working again is either to disconnect then reconnect the camera/board input wire or to recycle power to the micro.

When I separate the camera wire from the board, the program continues to work regardless of the pulse frequency.

I have tried SYSTEM_THREAD(ENABLED) and it has improved the problem but has not eliminated it.

I have already checked with the camera manufacturer. The camera digital input lines are opto-isolated and do not send anything back.

Can you help me find out what the problem is? For example, if I wire the camera digital inputs directly to the Photon?

Thank you

@Jimmie, can you post your code?

Thank you. Here it is:

#include "LIDARLite.h"
#include <Wire.h>

SYSTEM_THREAD(ENABLED);

LIDARLite myLidarLite;

double Distance = 0;
double Distance1 = 0;
int iDist = 0;

int ipub = 0 ;
int Hammo =  0 ;
int brewCoffee(String command);

int photoresistor = A0; // This is where your photoresistor is plugged in.
int power = A3;

int Light;

int triggerPin = D7;    // Pin 4 on Shield

float triggerDist = 2;   //Trigger Distance

void setup()
{
  Serial.begin(9600); // Initialize serial connection to display distance readings

  pinMode(triggerPin, OUTPUT);

  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);

  pinMode(photoresistor,INPUT);  // Our photoresistor pin is input (reading the photoresistor)
  pinMode(power,OUTPUT); // The pin powering the photoresistor is output (sending out consistent power)

  digitalWrite(triggerPin, LOW);

  // Next, write one pin of the photoresistor to be the maximum possible, so that we can use this for power.
  digitalWrite(power,HIGH);

  digitalWrite(5, HIGH);

   //Particle.variable("Distance", Distance);
  Particle.variable("Distance", &Distance, DOUBLE);
  Particle.variable("Hammo", &Hammo, INT);
  Particle.variable("light", &Light, INT);
  
  Particle.function("brew", brewCoffee);

  myLidarLite.begin(0, true); // Set configuration to default and I2C to 400 kHz

  myLidarLite.configure(0); // Change this number to try out alternate configurations
  //myLidarLite.configure(3); // Change this number to try out alternate configurations
}

void loop()
{

  Light = analogRead(photoresistor);


  // Take a measurement with receiver bias correction and print to serial terminal
  Distance1=(0.0328 * myLidarLite.distance());
  iDist = Distance1 * 10. ;
  Distance = iDist/10.;

  Serial.print(Distance);
  Serial.print("  ");
  Serial.print(Light);
  Serial.print("  ");
  Serial.println(ipub);

  if (Distance < 150)
  {
    // Take 99 measurements without receiver bias correction and print to serial terminal
  for(int i = 0; i < 99; i++)
  {
  Distance1=(0.0328 * myLidarLite.distance(false));
  iDist = Distance1 * 10. ;
  Distance = iDist/10.;

  Serial.print(Distance);
  Serial.print("  ");
  Serial.print(Light);
  Serial.print("  ");
  Serial.println(ipub);
  
  delay(1);
  }
  //}
  
  if (Hammo ==7)        //Trigger Mode
 {
     if (Distance < 4)
     {
  digitalWrite(triggerPin, HIGH);
  delay(10);
  digitalWrite(triggerPin, LOW);
  delay(500);
     }
 }

if (ipub == 666)  System.reset();


 if (ipub ==1)    //Publish
 {
 digitalWrite(3, LOW);
 digitalWrite(2, HIGH);
 digitalWrite(5, LOW);

 Spark.publish("Distance", String(Distance) + " ft");
 Spark.publish("Hammo", String(Hammo) );
 Spark.publish("Light", String(Light) );

 delay(2500);

 digitalWrite(3, LOW);
 digitalWrite(2, LOW);
 digitalWrite(5, HIGH);

 delay(2500);
}

   delay (1);
}
}

void triggerCamera()
{
  digitalWrite(triggerPin, HIGH);
  delay(100);
  digitalWrite(triggerPin, LOW);
  delay(100);
}

int brewCoffee(String command)
{
     Hammo = command.toInt();
     ipub = command.toInt();
}

@Jimmie, let’s start with these items:

  1. This line is not required:
#include <Wire.h>
  1. You need to use proper Particle pin values here (D1, A1, etc):
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
...
  digitalWrite(5, HIGH);

  1. You need to read up on the correct syntax for Particle.variable():
   //Particle.variable("Distance", Distance);
  Particle.variable("Distance", &Distance, DOUBLE);
  Particle.variable("Hammo", &Hammo, INT);
  Particle.variable("light", &Light, INT);
  1. These command (Spark) are deprecated and you need to use Particle. Furthermore, doing 3 publishes in a row violates the 1 publish per second rule and will cause the Cloud to choke back your publish requests. Take a look at the documentation. You could combine everything into a single publish using sprintf().
 Spark.publish("Distance", String(Distance) + " ft");
 Spark.publish("Hammo", String(Hammo) );
 Spark.publish("Light", String(Light) );
  1. This code will essentially set both Hammo and iPub to the same value. Was this intended? You need to read up on parsing a command string if you expected 2 arguments to be parsed.
int brewCoffee(String command)
{
     Hammo = command.toInt();
     ipub = command.toInt();
}

You have a LOT of delay() calls in loop(). You may want to consider using non-blocking delays. Can you provide a link to the camera trigger board?

Hello peekay123:

Thank you for your time, I appreciate it.

As you can tell, I am new to this :-).

I will make all the changes you recommended but if these were the culprit, why is it when the camera input lines are not connected everything works?

The camera board is available here (page 46 ):

http://www.ptgrey.com/support/downloads/10264/

Thanks again,

Jimmie

Thanks, found soft delay syntax.

@Jimmie, here is a tutorial as well.

Can you show (schematic or picture) how you have everything connected to your camera trigger?

Thank you peekay123.

I am on my way to the airport so I will do the soft delay later.

Initially, the camera optoinputs were connected to the Particle Shield GND and the input was connected to pin 7 on the shield. That is when i was having loss of internet connection.

However, I have changed and connected now the camera input directly to D7 on the Photon and the problem seems to go away.

  1. My current code is below:
#include "LIDARLite.h"

SYSTEM_THREAD(ENABLED);

LIDARLite myLidarLite;

unsigned long old_time = 0;

String stringOne;

double Distance = 0;
double Distance1 = 0;
int iDist = 0;

int ipub = 0 ;
int brewCoffee(String command);

int photoresistor = A0; // This is where your photoresistor is plugged in.
int power = A3;

int Light;

int triggerPin = D7;    // Pin 4 on Shield

float triggerDist = 2;   //Trigger Distance

void setup()
{
  Serial.begin(9600); // Initialize serial connection to display distance readings

  pinMode(triggerPin, OUTPUT);

  pinMode(D3, OUTPUT);
  pinMode(D2, OUTPUT);
  pinMode(D5, OUTPUT);     //Green

  pinMode(photoresistor,INPUT);  // Our photoresistor pin is input (reading the photoresistor)
  pinMode(power,OUTPUT); // The pin powering the photoresistor is output (sending out consistent power)

  digitalWrite(triggerPin, LOW);

  // Next, write one pin of the photoresistor to be the maximum possible, so that we can use this for power.
  digitalWrite(power,HIGH);

  digitalWrite(D5, HIGH);

  Particle.variable("stringOne", stringOne);
  //Particle.variable("Distance", Distance);
 // Particle.variable("ipub", &ipub, INT);
  //Particle.variable("ipub", ipub);
  //Particle.variable("light", Light);
  
  Particle.function("brew", brewCoffee);

  myLidarLite.begin(0, true); // Set configuration to default and I2C to 400 kHz

  myLidarLite.configure(0); // Change this number to try out alternate configurations
  //myLidarLite.configure(3); // Change this number to try out alternate configurations
}

void loop()
{

  Light = analogRead(photoresistor);


  // Take a measurement with receiver bias correction and print to serial terminal
  Distance1=(0.0328 * myLidarLite.distance());
  iDist = Distance1 * 10. ;
  Distance = iDist/10.;

  Serial.print(Distance);
  Serial.print("  ");
  Serial.print(Light);
  Serial.print("  ");
  Serial.println(ipub);

  if (Distance < 150)
  {
    // Take 99 measurements without receiver bias correction and print to serial terminal
  for(int i = 0; i < 99; i++)
  {
  Distance1=(0.0328 * myLidarLite.distance(false));
  iDist = Distance1 * 10. ;
  Distance = iDist/10.;

  Serial.print(Distance);
  Serial.print("  ");
  Serial.print(Light);
  Serial.print("  ");
  Serial.println(ipub);
  
  delay(1);
  }
  //}
  
  if (ipub ==7)        //Trigger Mode
 {
     if (Distance < 4)
     {
  digitalWrite(triggerPin, HIGH);
  delay(10);
  digitalWrite(triggerPin, LOW);
  delay(500);
     }
 }

if (ipub == 666)  System.reset();


 if (ipub ==1)    //Publish
 {
 digitalWrite(D3, LOW);
 digitalWrite(D2, HIGH);
 digitalWrite(D5, LOW);

 
 stringOne=String(Distance)  +  " ft   " + String(ipub)  +  "    " +  String(Light);
 Particle.publish("Results", String(stringOne) );
 
 //Particle.publish("Distance", String(Distance) + " ft");
 //Particle.publish("ipub", String(ipub) );
//Particle.publish("Light", String(Light) );

 delay(2500);

 digitalWrite(D3, LOW);
 digitalWrite(D2, LOW);
 digitalWrite(D5, HIGH);

 delay(2500);
}

   delay(1);
}
}

void triggerCamera()
{
  digitalWrite(triggerPin, HIGH);
  delay(100);
  digitalWrite(triggerPin, LOW);
  delay(100);
}

int brewCoffee(String command)
{
     ipub = command.toInt();
}