Light sensor trigger

Hi
I been trying to nut this out but I am new to Arduino, my last programming was 30 years ago.
I want to trigger when a sudden change is detected by the TEMP6000. I used the average example in the reference as a base extended over 10 samples. I need to get rid of the manually entered ‘8230’ to cope with changes in ambient light. I thought of single pin read multiplied to give a figure to compare to, however I keep getting 0 for the pin. Any suggestions? The HIGH output pin will be reset LOW later on.

// setup a buffer that averages the last ten samples of a sensor
int sensPin = 0;
int sensVal[10]; // create an array for sensor data
int i, j; // counter variables
long average; // variable to store average

void setup (){
Serial.begin(9600);
}

void loop(){
// input sensor data into oldest memory slot
sensVal[(i++) % 10] = analogRead(sensPin);
average = 0;
for (j=0; j<10; j++){
average += sensVal[j]; // add up the samples
}

Serial.println (average);

if (average > 8230)
  digitalWrite (12, HIGH);

}

Many thanks
Andy
NZ

What is TEMP6000 ?

with your averaging, you will need to divide by 10. The average will be over a very short time interval, FYI.

Check your SENSOR Pin, if you are using serial, you need to move the sensor to digital pin2 or higher; if you are using an analog pin, you need to be on A0 or higher.

// setup a buffer that averages the last ten samples of a sensor
int sensPin = 0;  // should not use this if you are writing to serial.
int sensVal[10]; // create an array for sensor data
int i; // counter variables
int total;
int average; // variable to store average
unsigned long startTime = millis();

void setup ()
{
  Serial.begin(9600);   //you are using serial here...
  pinMode(12, OUTPUT);  //
  //pinMode(sensPin, INPUT);  //don't forget this if you intend to get input from a digital pin
}

void loop()
{
  sensVal[(i++) % 10] = analogRead(sensPin); // input sensor data into oldest [array] slot
  total = 0;
  for (int j = 0; j < 10; j++)
  {
   total += sensVal[j]; // add up the samples
  }
  average = total/10;  //returns the average
  Serial.println (average);
  if (average > 8230) //this is set to your threshold value
  {
   flash12(); 
  }
  else
  {
    digitalWrite (12, LOW);
  }
}

void flash12()
{
  if (millis() - startTime >= 250UL)
  {
    digitalWrite( 12, !digitalRead(12));
    startTime = millis();
  }
}

Thanks BulldogLowell
I don't understand the purpose of the millis timer.

Thanks again
Andy

In your sketch you don't set pin 12 as output so doing the digitalWrite(12,HIGH) is just turning on the internal pullup resistor for that pin.

Do you need to just detect sudden changes in level but compensate for varying ambient levels of detect when the level goes over a certain value?

The below untested code will (hopefully) do a rolling average to compensate for ambient but if the value alters more than a certain value in a single step then it will trigger.

#define differenceTrigger 50  // How much a reading must differ by to trigger

const int sensPin = A0;       // Sensor input pin

int rollingAverage;           // Used to store rolling average of sensor

void setup (){
  Serial.begin(9600);
  rollingAverage = analogRead(sensPin); // Pre load average so we don't trigger on first read
}

void loop(){
  int currentReading = analogRead(sensPin);
  
  Serial.print(rollingAverage);
  Serial.print("\t");
  Serial.print(currentReading);
  
  int tempResult = currentReading - rollingAverage; // Needed to ensure abs() works correctly
  if (abs(tempResult) >= differenceTrigger){        // Has it jumped more than trigger value?
    Serial.print("\t");
    Serial.print("Triggered.");
  }
  Serial.println();
  
  rollingAverage = (rollingAverage + currentReading) / 2; // Compute new rolling average
}

andy120:
Thanks BulldogLowell
I don't understand the purpose of the millis timer.

Thanks again
Andy

only to make the LED blink, but mostly to show you that you needed something to turn the led off, which you missed in your code.

I see, many thanks again