Compare present reading with the last sensor readings

Hi there.,

still new to adruino and am experimenting with a capacitive sensor to control the brightness of an LED.
it works fine and the LED lights up or dims with the sensor reading.
now i wanted to change the programming so that the LED brightness stays at the highest level of the sensor readings. in a way like this it is only possible to make the LED brighter and it stays at that level.
if a certain level is reached or maybe with a reset button the LED should be resettable to 0 though.

among other things, i guess i need a to compare the newest sensor value to the last sensor value.

any ideas?

here is my code so far (sorry the coding i s a bit complicated since i needed to add the capacitive sensor library and i added a smoothing average to the sensor readings):

#include <CapacitiveSensor.h>
CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2);
int ledPin = 9;
const int numReadings = 4;

int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0;

void setup()
{
Serial.begin(9600);
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);
pinMode(ledPin, OUTPUT);
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;

}

void loop()
{
long start = millis();
long sensor = cs_4_2.capacitiveSensor(30);

// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = sensor;
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;

if (index >= numReadings)
index = 0;
average = total / numReadings;
delay(1);

int val = average;
val = constrain(val, 160, 7000);
int ledLevel = map(val, 7000, 160, 255, 0);

Serial.print(millis() - start);
Serial.print("\t");
Serial.print(sensor); // print sensor output 1
Serial.print("\t");
Serial.print(average);
Serial.print("\t");
Serial.println(val);
delay(50);

analogWrite(ledPin, ledLevel);

}

many thanks in advance!!!

Read the how to use the forum sticky and modify that code to use the correct code tags.

You need to have a variable called say lastSensorValue and the last thing in the loop function you make it equal to the current reading. Then when you take a new reading next time round the loop you have a value to compare it to.

Read this before posting a programming question

It is very hard to tell with your layout.

http://www.gammon.com.au/forum/?id=12153#style

Thank you for your quick replies!

I actually figured out a solution by myself now.
The objective was:

  1. LED brightness increases proportional to higher sensor values
  2. LED brightness remains at max sensor values. (even if sensor values decline again)
  3. After surpassing a defined threshold LED brightness is set back to 0.

this is the code:

#include <CapacitiveSensor.h>
CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2);
int ledPin = 9;
const int numReadings = 4;
const int numReadings2 = 2;

int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0;
int readings2[numReadings2];
int index2 = 0;
int total2 = 0;
int average2 = 0;
int val = 0;

void setup()
{
Serial.begin(9600);
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);
pinMode(ledPin, OUTPUT);
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;

}

void loop()
{
long start = millis();
long sensor = cs_4_2.capacitiveSensor(30);

total= total - readings[index];
readings[index] = sensor;
total= total + readings[index];
index = index + 1;

if (index >= numReadings)
index = 0;
average = total / numReadings;

total2= total2 - readings2[index2];
readings2[index2] = average;
total2= total2 + readings2[index2];
index2 = index2 + 1;

if (index2 >= numReadings2)
index2 = 0;
average2 = total2 - average;

if (average >= average2 && val <average)
val = average;
delay(1);

val = constrain(val, 100, 3000);
int ledLevel = map(val, 3000, 100, 255, 0);

Serial.print(sensor);
Serial.print("\t");
Serial.print(average);
Serial.print("\t");
Serial.print(average2);
Serial.print("\t");
Serial.println(val);
delay(50);

analogWrite(ledPin, ledLevel);

if (val >=3000)
val = 0;

}

If you persist in not giving us the curtsy of posting your code correctly you will not win many friends here.

 if (average >= average2 && val <average)
  val = average;
  delay(1);
 
 
    val = constrain(val, 100, 3000);
    int ledLevel = map(val, 3000, 100, 255, 0);
   
             
    Serial.print(sensor);                 
    Serial.print("\t");
    Serial.print(average); 
    Serial.print("\t");
    Serial.print(average2);
    Serial.print("\t");
    Serial.println(val);
  delay(50);

All that indenting makes it almost impossible to follow. And I am referring to you as well as me.