code not repeating and issue with second timer

// variables below

long pwm1 = 5; //pwm1 to be 5% of pwm2
long pwm2 = 7;//pwm2 maximum

long rampuptime = 5000; //time to ramp up to pwm level 
long ontime1 = 15000; //on time for cell, can be 1minute up to full on for days
long switchtime = 10000; //time to switch between ledfwd and ledrvs
long acctimeon = 30000; //how often to start cycle
long cycle = 20000; //cycles between start times
//variables above


const int ledfwd = 11;
const int ledrvs = 10;
const int ledrly = 12;
const int ledstop = 8;
const int ledrun = 9;

long pwm3 = pwm2 - pwm1; //difference

unsigned long previousMillis = 0;

long interval1 = rampuptime / pwm1; //millis per fade
long interval2 = rampuptime / pwm3;//millis per fade
long ontime2 = 0;
int brightness1 = 0; //fade brightness accumulated
int fadeAmount = 1; //change to 1 for final code
int on = 0;
long acctime = 0;
unsigned long currentMillis = millis();
int led =  ledrvs;



void setup() {

  Serial.begin(9600);
  int brightness = 0;
  pinMode(led, OUTPUT);
}

void loop()
{ currentMillis = millis();
 if (brightness1 == 0)
  {digitalWrite (ledstop, HIGH);}
   else {digitalWrite (ledstop, LOW);};
  start();
}



void rampup1()
{
  analogWrite(led, brightness1);
  analogWrite(ledrun, brightness1);
 if (brightness1 <= pwm1)
  {
    Serial.println("ramp1");
    brightness1 = constrain(brightness1, 0, pwm1);
    Serial.println((String)"BRIGHTNESS1:" + brightness1 + " Time:" + currentMillis);
    if (currentMillis - previousMillis >= interval1) {
      previousMillis = currentMillis;
      brightness1 = brightness1 + fadeAmount;
    }
  }
  else if (brightness1 >= pwm1 && brightness1 < pwm2)
  {
    Serial.println("ramp2");
    brightness1 = constrain(brightness1, pwm1, pwm2);
    Serial.println((String)"BRIGHTNESS1:" + brightness1 + " Time:" + currentMillis);
     if (currentMillis - previousMillis >= interval2) {
      previousMillis = currentMillis;
      brightness1 = brightness1 + fadeAmount;
    } 
}
else  {
  previousMillis = currentMillis;
  brightness1 = pwm2;
Serial.println((String)"BRIGHTNESS1:" + brightness1 + " Time:" + currentMillis);
  
}
}
void switch1 ()
{
    if (ontime1 >= switchtime)
  {
    led = ledfwd;
  }
  else {
    led = ledrvs;
  }

}
void start ()
{
 
  if (currentMillis - ontime2 <= ontime1)
  {
    rampup1();
  }
  else if (currentMillis > ontime1 && currentMillis < cycle){
    brightness1 = 0;
    analogWrite(led, brightness1);
    analogWrite (ledrun, brightness1);
    Serial.println((String)"BRIGHTNESS1:" + brightness1 + " Time:" + currentMillis);}
else 
{ontime2 = currentMillis;
 cycle = currentMillis;
}
}

code as above, works fine except 2 things

  1. the code only repeats once, would like indefinite repeats
  2. trying to make second timer but am wanting help. will update code as I make it

please help with first part, and any ideas for second are appreciated

ok the timer is harder than i thought.
I was hoping to store when led changes state from low to high then take that away from when it goes high to low giving a time i could reference.
no luck so far. please anyone?
also having issues with this running twice then nothing, am hoping to get it running over and over again

freerpg:
also having issues with this running twice then nothing, am hoping to get it running over and over again

1. Do you want to say that the loop() function is not repeating? It does repeat; insert this code (Serial.println("OK...!"); in the loop() function after the start(); and check that the message (OK...!) is appearing repeatedly in the Serial Monitor.

2.

trying to make second timer but am wanting help. will update code as I make it

What is this 'second timer'? How do want to make it? What will it do?

3. Tell us something in descriptive way: What is the purpose of your program? If we know the purposes, we can follow your implementation mechanism (the codes); otherwise, we appear to be totally dumb except pointing out some syntax errors in your program.

// variables below

long pwm1 = 5; //pwm1 to be 5% of pwm2
long pwm2 = 7;//pwm2 maximum

long rampuptime = 5000; //time to ramp up to pwm level 
long ontime1 = 15000; //on time for cell, can be 1minute up to full on for days
long switchtime = 10000; //time to switch between ledfwd and ledrvs
long acctimeon = 30000; //how often to start cycle
long cycle = 20000; //cycles between start times
//variables above


const int ledfwd = 11;
const int ledrvs = 10;
const int ledrly = 12;
const int ledstop = 8;
const int ledrun = 9;

long pwm3 = pwm2 - pwm1; //difference

unsigned long previousMillis = 0;

long interval1 = rampuptime / pwm1; //millis per fade
long interval2 = rampuptime / pwm3;//millis per fade
long ontime2 = 0;
int brightness1 = 0; //fade brightness accumulated
int fadeAmount = 1; //change to 1 for final code
int on = 0;
long acctime = 0;
long acctimer = 0;
long startacctimer = 0;
long endacctimer = 0;
long dif = 0;
int state = HIGH;

unsigned long currentMillis = 0;
int led =  ledfwd;
unsigned long ledStartTime = 0;
unsigned long ledFinishTime = 0;
unsigned long ledTime = 0;


void setup() {

  Serial.begin(9600);
  int brightness = 0;
  pinMode(led, OUTPUT);
}

void loop()
{ currentMillis = millis();
  
 digitalWrite (ledstop, state);
  switch1();
 
}



void rampup1()
{
  analogWrite(led, brightness1);
  analogWrite(ledrun, brightness1);
 if (brightness1 <= pwm1)
  {
    Serial.println("ramp1");
    brightness1 = constrain(brightness1, 0, pwm1);
    Serial.println((String)"BRIGHTNESS1:" + brightness1 + " Time:" + currentMillis + "acctimer:" + acctimer +"ledFinishTime" + ledFinishTime + "ledStartTime" + ledStartTime);
    if (currentMillis - previousMillis >= interval1) {
      previousMillis = currentMillis;
      brightness1 = brightness1 + fadeAmount;
    }
  }
  else if (brightness1 >= pwm1 && brightness1 < pwm2)
  {
    Serial.println("ramp2");
    brightness1 = constrain(brightness1, pwm1, pwm2);
    Serial.println((String)"BRIGHTNESS1:" + brightness1 + " Time:" + currentMillis + "acctimer:" + acctimer +"ledFinishTime" + ledFinishTime + "ledStartTime" + ledStartTime);
     if (currentMillis - previousMillis >= interval2) {
      previousMillis = currentMillis;
      brightness1 = brightness1 + fadeAmount;
    } 
}
else  {
  previousMillis = currentMillis;
  brightness1 = pwm2;
Serial.println((String)"BRIGHTNESS1:" + brightness1 + " Time:" + currentMillis + "acctimer:" + acctimer +"ledFinishTime" + ledFinishTime + "ledStartTime" + ledStartTime);
  
}
}


void start ()

{
 
  if (currentMillis - ontime2 <= ontime1)
  {
    rampup1();
  }
  else if (currentMillis > ontime1 && currentMillis < cycle){
    brightness1 = 0;
    analogWrite(led, brightness1);
    analogWrite (ledrun, brightness1);
    Serial.println((String)"BRIGHTNESS1:" + brightness1 + " Time:" + currentMillis + "acctimer:" + acctimer +"ledFinishTime" + ledFinishTime + "ledStartTime" + ledStartTime);}
else 
{ontime2 = currentMillis;
 cycle = currentMillis;
}
}

void switch1 ()
{if (brightness1 == 0)
  {endacctimer = millis();
   state = HIGH;
   ledFinishTime = currentMillis;
   
  }
   else {startacctimer = millis(); 
         state = LOW;
     ledStartTime = currentMillis;
        };
  start();
 if (ledFinishTime - ledStartTime >= acctimeon)
 { 
   led = ledfwd; 
 }
 
 else {
 led = ledrvs;
 }
  }

ok timer sort of works. ledstartTime and ledFinishtime conitnually update. want it to only get the time once when pin 8 goes from high to low (starttime) and once from low to high (finishtime) any ideas?

GolamMostafa:
1. Do you want to say that the loop() function is not repeating? It does repeat; insert this code (Serial.println("OK...!"); in the loop() function after the start(); and check that the message (OK...!) is appearing repeatedly in the Serial Monitor.

2. What is this 'second timer'? How do want to make it? What will it do?

3. Tell us something in descriptive way: What is the purpose of your program? If we know the purposes, we can follow your implementation mechanism (the codes); otherwise, we appear to be totally dumb except pointing out some syntax errors in your program.

  1. yeah the loop repeats but not the function rampup1 which is called in start which is called in switch which is call inloop

  2. second timer tells how long between state switch on pin 8, variable is called "state" to make things easier

  3. device to work as follows

A) when on turns on "LED". "LEDSTOP" off
B) "LED" on for "ONTIME1"
C) "LED" off until "CYCLE" complete. "LEDSTOP" activates
Repeat

When LED accumulates a preset time "ACCTIMEON" LED turns off.
LED value changes from LEDFWD to LEDRVS or LEDRVS to LEDFWD depending on which is on.
count until a new variable "RELAY" is reached (probably2000 at this time) then "LEDRLY" turns on for "RELAY" period then off for "RELAY" period.
start at A) above

Hi,
So you got it working, why make a new thread and leave the other hanging?

Tom... :slight_smile: