Hi everyone, quite a novice coder here. I'm working on a project for a physical computing class that has been throwing me for a loop for a week or so now. I'm trying to use cap touch sensors to individually trigger some LEDs and have them fade from low-high-low in a 5 second time span. What makes this code tricky is that I'm trying to fake multi-threading so I can trigger more than one fade at the same time when the sensor is tapped. I've figured out how to keep the fade going through the five second interval using a cosine function and some annoying boolean code.
int value, value2 ;
int ledpin = 2;
int ledpin2 = 6;
long time=0;
int periode = 5000;
int displace = 500;
int cap1 = A2;
int cap2 = A3;
int cap3 = A4;
int cap4 = A5;
int cap5 = A6;
boolean button1 = false;
unsigned long currenttime1 = 0;
void setup()
{
Serial.begin(9600);
pinMode(ledpin, OUTPUT);
pinMode(ledpin2, OUTPUT);
pinMode(cap1, INPUT_PULLUP);
pinMode(cap2, INPUT_PULLUP);
pinMode(cap3, INPUT_PULLUP);
pinMode(cap4, INPUT_PULLUP);
pinMode(cap5, INPUT_PULLUP);
}
void loop()
{
time = millis();
value = 128+127*cos(2*PI/periode*time);
value2 = 128+127*cos(2*PI/periode*(displace-time));
if(millis() > (currenttime1 + 5000)){
button1 = false;
}
if(button1 == false){
if(digitalRead(cap4) == HIGH){
button1 = true;
currenttime1 = millis();
}
}
if(button1 == true){
analogWrite(ledpin, value); // sets the value (range from 0 to 255)
}
My problem is, since this cosine equation uses the millis() function, it will start to fade from some random value in between 0-255 because of the millis. I would really like this to start from 0 and ramp to 255 and then back to 0 again. Is there any way I can constrain these values to only begin the fade starting at 0?
Thanks for your help everyone.