Reading State

Hello all,
I am having some problems with the code I am writing and wondering if anyone can offer help. What I am trying to do is use a digital read of the TEMT6000 sensor to display a state of either 0 (dark) or 1 (light). I am not having problem’s with the sensor or the sensor code but the output the sensor produces. I’m trying to have a variable add one to the counter every time there is a state change from 0 to 1. I tried nested if…else statements but that had no success. I figured that a switch case would be better with an if statement built-in but it seems to ignore the if statement. Any suggestions?

#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>

int temt6000Pin = 2; // ALS in Digital Pin 2
int LEDPin = 13;  //  LED in Digital Pin 13
int pos=0; /* time_rev, time_total, num_rev; */ // Variables for calculations for later

void setup() {
  Serial.begin(9600);
    setSyncProvider(RTC.get);  // Gets time from the RTC
    pinMode(LEDPin, OUTPUT);  // LED is an output from the board
    pinMode(temt6000Pin, INPUT);  // ALS is a input from the enviroment
}

void loop() {
  int value = digitalRead(temt6000Pin);  // digital read of ALS
   //Serial.println(value);  // Display state of ALS, 1 for light, 0 for no light
   switch (value){
     
     case 0:
     digitalWrite(LEDPin, LOW);
     break;
     
     case 1:
     digitalWrite(LEDPin, HIGH);
     if(value == 0){
       pos++;
       Serial.println(pos);
       if(pos >= 4){
         pos = 0; 
       }
     }
     break;
   }

}

I'm trying to have a variable add one to the counter every time there is a state change from 0 to 1.

The only way to detect a state change is to keep track of the previous state. That requires a global or static variable, which you do not have.

I tried nested if...else statements but that had no success. I figured that a switch case would be better with an if statement built-in but it seems to ignore the if statement.

Your not even barking in the right forest, let alone the right tree.

Any suggestions?

There's one up top.

PaulS:

The only way to detect a state change is to keep track of the previous state. That requires a global or static variable, which you do not have.

How do you use a global variable?

 switch (value){
     
     case 0:
     ...
     break;
     
     case 1:
     ...
     if(value == 0){

How could this possibly be true? If value is 1, then you go to "case 1". So value is hardly going to be zero is it?

How do you use a global variable?

Just like a local variable. In your sketch, LEDPin is a global variable.

int currState;
int prevState = HIGH;

void loop()
{
   currState = digitalRead(temt6000Pin);
   if(currState != prevState) // a transition occurred
   {
      pos++;
   }
}

PaulS:

How do you use a global variable?

Just like a local variable. In your sketch, LEDPin is a global variable.

int currState;

int prevState = HIGH;

void loop()
{
  currState = digitalRead(temt6000Pin);
  if(currState != prevState) // a transition occurred
  {
      pos++;
  }
}

What your saying makes sense, the problem that I am running into is that the state change last's longer than one instant so that comparing the currState to prevState (which is defined as HIGH) add's one to the counter (pos) as long as the interruption of light occurs. I'm looking to try and only look at the the transition from HIGH to LOW and not all of the LOW state.

Oops. The assignment of currState to prevState is missing.
int currState;
int prevState = HIGH;

void loop()
{
currState = digitalRead(temt6000Pin);
if(currState != prevState) // a transition occurred
{
pos++;
}
prevState = currState;
}

Thanks so much for your help.