Photoresistors with laser doesn't trigger if laser is cut too quick

Hi guys, I’m using a Photo-resistor and laser to trigger a http get. Everythign works if i cut the laser for 1 second or more but I’m having an issue where the Photo-resistors doesn’t trigger if the laser is cut and then back on faster.

The Photo-resistors is connected to a digital pin on my photon, should it be on a analog pin instead ? Would that allow it to be more sensitive to change ?

Thanks

Could you share the code you’re using?

@sebastienb, a photo-resistor does not react instantly, especially if it is less sensitive to the laser’s color. Because of that, the logic ‘HIGH’ threshold voltage will take longer to reach. Have you considered using a photo-transistor instead?

Here is a link to the code I’m using. http://codeshare.io/0IjFm

I’ve used the same Photoresistors before but using an Arduino uno with some slightly different code. It might have been using the analogue pins.

1 Like

its easier for folks if you post your code here:

/**
 * This is a simple "sketch" that calls a web page when a button is pressed.  It is a proof-of-concept
 * for my boss and hastily written in about 2 hours.  The remote URL is a PHP script that handles
 * making the API calls to a remote SMS-messaging service.
 * 
 * I'm sure it could make use of interrupts somehow, but I'm not sure how off the top of my head.
 * 
 * It uses the onboard RGB LED as status display:
 *  - Red = Waiting to be pressed
 *  - Green = Making HTTP request
 *  - Blue = Finished HTTP request
 * 
 * Hardware:
 *  - Normally-off momentary button
 *  - Spark Core
 * 
 * Setup:
 *  - Connect one pin of the button to GND
 *  - Connect the other pin of the button to the desired digital pin on the Spark Core
 * 
 * The configuration is in the variables defined at the top of the script.  They should be pretty
 * self-explanatory.
 **/

int millLast = 0;                    // Last time (in millis()) the doorbell was pressed
int blueTeam = D0;                // blue team pin #
int redTeam = D1;                // red team pin #

int millDelay = 100;               // How much time to wait until we allow another doorbell "ring" again
char srvIP[] = "10.0.0.251";            // Server IP address
char srvHost[] = "http://bobafett.local"; // HTTP 1.1 hostname
int srvPort = 3000;                    // Server port number
char srvPathBlue[] = "/game/blueplus";       // URL path
char srvPathRed[] = "/game/redplus";       // URL path

void setup() {
    pinMode(blueTeam, INPUT_PULLUP);
    pinMode(redTeam, INPUT_PULLUP);

    RGB.control(true);
}

void loop() {
    if(digitalRead(blueTeam)==HIGH)
        bluePointPlus();
    else if(digitalRead(redTeam)==HIGH)
        redPointPlus();
    else
        RGB.color(255, 0, 0);
}


void bluePointPlus() {
    if(millLast==0 || (millLast+millDelay)<millis()) {
        millLast = millis();
        
        RGB.color(0, 255, 0);
        httpGetRequest(srvIP, srvHost, srvPort, srvPathBlue);
        RGB.color(0, 0, 255);
        
        delay(1000);
    }
}

void redPointPlus() {
    if(millLast==0 || (millLast+millDelay)<millis()) {
        millLast = millis();
        
        RGB.color(0, 255, 0);
        httpGetRequest(srvIP, srvHost, srvPort, srvPathRed);
        RGB.color(0, 0, 255);
        
        delay(1000);
    }
}


void httpGetRequest(char* ip, char* hostname, int port, char* url) {
    char line[255];
    
    TCPClient client;
    client.connect(ip, port);

    strcpy(line, "GET ");
    strcat(line, url);
    strcat(line, " HTTP/1.1");
    client.println(line);
    delay(100);

    strcpy(line, "Host: ");
    strcat(line, hostname);
    client.println(line);
    delay(100);
    
    strcpy(line, "Content-Length: 0");
    client.println(line);
    delay(100);
    
    client.println();
    delay(100);
    
    client.flush();
    delay(100);
    
    client.stop();
    delay(250);
}

You have some blocking code in your functions -> delay(1000) <- that may prevent your code from responding during that delay.

you can study the classic state change example and consider using that… before you consider an interrupt:

const int  buttonPin = D2;    // the pin that the pushbutton is attached to
const int ledPin = D7;       // the pin that the LED is attached to

int lastButtonState = 0;     // previous state of the button

void setup()
{
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  int buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState)
  {
    if (buttonState == HIGH)
    {
      Serial.println("on");
    }
    else
    {
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(25);
  }
  lastButtonState = buttonState;
}









The delay is only triggered if it detects a state change which would be fine. I will look at what you suggested.