code for up scale

int ledfwd = 11;
int ledrvs = 10;
int led =  ledrvs;
int ledrly = 12;
         

long rampuptime = 5000;

long pwm1 = 5; //pwm1
long pwm2 = 20;//pwm2 max


unsigned long previousMillis = 0;   

long interval1 = rampuptime / pwm1;    
long interval2 = rampuptime / pwm2;
long ontime1 = 15000;
long switchtime = 10000;
long turnon = 30000;
int brightness1 = 0;    
int fadeAmount = 1; //change to 1 for final code
int on = 0;

unsigned long currentMillis = millis();


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

void loop()
{currentMillis = millis();

  start();
  
   
}



void rampup1()
{ 
   analogWrite(led, 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);}
 
 {
   brightness1 = pwm2;
   
 }
   
    if (currentMillis - previousMillis >= interval2) {
   previousMillis = currentMillis;
  
     brightness1 = brightness1 + fadeAmount;
   }
      }


void switch1 ()
{
if (ontime1 >= switchtime)
{ led = ledfwd;}
  else {led = ledrvs;}
  
}

void start ()
{
  if (currentMillis >= on)
  {rampup1();}
  else if (currentMillis >= on && currentMillis <= ontime1)
  {brightness1 = pwm2;
 analogWrite(led, brightness1);}
  else (currentMillis > ontime1 && currentMillis <= turnon)
  ;{brightness1 = 0;
 analogWrite(led, brightness1);
   switch1 ();}
  
 }

the above code is SUPPOSED to turn on the ramp up (which works) but the void start statement is not turning it on at all.... well it does but keeps pwm at 0.
the idea is that if current millis is more then on (0) and less then ontime1 (15000) run the ramp up code
after that brightness = pwm2 and stays on untill offtime
after that pwm = 0 until next cycle

at the moment it keeps pwm at 0

Here is part of your code

  else (currentMillis > ontime1 && currentMillis <= turnon)
    ;
  {
    brightness1 = 0;
    analogWrite(led, brightness1);
    switch1 ();
  }

What is the expression in the else meant to do ? Should there be an if after the else ?

What is that semicolon doing at the end of the else ?
It is the only code that will be conditionally executed by the else. The code in the braces below it will be unconditionally executed

UKHeliBob:
Here is part of your code

  else (currentMillis > ontime1 && currentMillis <= turnon)

;
 {
   brightness1 = 0;
   analogWrite(led, brightness1);
   switch1 ();
 }



What is the expression in the else meant to do ? Should there be an if after the else ?

What is that semicolon doing at the end of the else ?
It is the only code that will be conditionally executed by the else. The code in the braces below it will be unconditionally executed

ok so if the ontime has finished (15 seconds elapsed)the led goes to low or 0 until the cycle ends (turnon which is 30seconds). switch1 is a work in progress

the ; at the end was put there because the compiler wouldn't compile without it
error as below
In function 'void start()':
92:3: error: expected ';' before '{' token

arduino ide has below error but same issue

C:\Users\freer\Documents\Arduino\sketch_feb24a\sketch_feb24a.ino: In function 'void start()':

sketch_feb24a:92: error: expected ';' before '{' token

   {brightness1 = 0;

   ^

exit status 1
expected ';' before '{' token

currentMillis > ontime1 && currentMillis <= turnonIt looks to me as though that should be

else if (currentMillis > ontime1 && currentMillis <= turnon)

still an issue there. seems to run the ramp up once only

Hi,

This may help;

What is your application?

Tom... :slight_smile:

got rid of the else statements so only the first if statement is there. still only runs rampup once.

application is a guy wants to turn on a device for (A) amount of time every (B) amount of time. when accumulated (A) = (C) then i have to turn off for a minute activate a relay for a minute and then off for a minute then switch to second device and start again.... struggling hard as the variables can be anywhere from 10 seconds to 1 hour and the guy is hard to reach.

the other reason i am doing this is I have BASIC grip on coding so want to figure it out better and this is an option for me to learn

I got the way he wants me to turn on the device now, (rampup works pefectly) now I need it to turn on for a time and then wait until next timer(up to an hour)
somehow while doing that keep adding all the ontimes until ontime = accumulated time and then switch to second device with the relay in there.

got rid of the else statements so only the first if statement is there. still only runs rampup once.

Please post the modified code

int ledfwd = 11;
int ledrvs = 10;
int led =  ledrvs;
int ledrly = 12;
         

long rampuptime = 5000;

long pwm1 = 5; //pwm1
long pwm2 = 20;//pwm2 max


unsigned long previousMillis = 0;   

long interval1 = rampuptime / pwm1;    
long interval2 = rampuptime / pwm2;
long ontime1 = 15000;
long switchtime = 10000;
long turnon = 30000;
int brightness1 = 0;    
int fadeAmount = 1; //change to 1 for final code
int on = 0;

unsigned long currentMillis = millis();


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

void loop()
{currentMillis = millis();

  start();
  
   
}



void rampup1()
{ 
   analogWrite(led, 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);}
 
 
   brightness1 = pwm2;
   
 }
} 
  else if (currentMillis - previousMillis >= interval2) {
   previousMillis = currentMillis;
  
     brightness1 = brightness1 + fadeAmount;
   }
      }


void switch1 ()
{
if (ontime1 >= switchtime)
{ led = ledfwd;}
  else {led = ledrvs;}
  
}

void start ()
{
  if (currentMillis >= 0 )
  {rampup1();}
  
  
 }

this code has an error which I can't figure out

Hi,
What model Arduino?
Tom... :slight_smile:

uno, might swap to nano later

Hi,
This function has mismatching { and }

void rampup1()
{
  analogWrite(led, 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);
  }


  brightness1 = pwm2;

}
}
else if (currentMillis - previousMillis >= interval2) {
  previousMillis = currentMillis;

  brightness1 = brightness1 + fadeAmount;
}
}

You need to check your logic and number of { and }.

Tom... :slight_smile:

Hi,
I'm not sure of the use of this function;

void start()
{
  if (currentMillis >= 0 )
  {
  rampup1();
  }
}

currentMillis will always be greater than zero.
Can you tell us the significance of these variables, that is what do they represent in the timing?

long ontime1 = 15000;
long switchtime = 10000;
long turnon = 30000;

Tom... :slight_smile:

int ledfwd = 11;
int ledrvs = 10;
int led =  ledrvs;
int ledrly = 12;
         

long rampuptime = 5000;

long pwm1 = 5; //pwm1
long pwm2 = 20;//pwm2 max


unsigned long previousMillis = 0;   

long interval1 = rampuptime / pwm1;    
long interval2 = rampuptime / pwm2;
long ontime1 = 15000;
long switchtime = 10000;
long turnon = 30000;
int brightness1 = 0;    
int fadeAmount = 1; //change to 1 for final code
int on = 0;

unsigned long currentMillis = millis();


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

void loop()
{currentMillis = millis();

 rampup1();
  
   
}



void rampup1()
{ 
   analogWrite(led, 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);}
 
 
   brightness1 = pwm2;
} }
else if (currentMillis - previousMillis >= interval2) {
   previousMillis = currentMillis;
  
     brightness1 = brightness1 + fadeAmount;
   }
}



void switch1 ()
{
if (ontime1 >= switchtime)
{ led = ledfwd;}
  else {led = ledrvs;}
  
}

void start ()
{
  if (currentMillis <= ontime1)
  {rampup1();}
  
  
 }

why won't this compile now, i messed up somewhere, just cant see where

Hi,
Looking at what you need;

State 1, From startup to ontime1 (15000) ramp up an output to PWM1 level.
State 2, After ontime, output brightness at PWM2 level.
State 3, At offtime turn off output.
State 4, Stay off.

I assume you want it to only run this cycle once, resetting or turning the controller off then on with restart the process?
How fast is the rampup?

Tom.... :slight_smile:

TomGeorge:
Hi,
Looking at what you need;

State 1, From startup to ontime1 (15000) ramp up an output to PWM1 level.
State 2, After ontime, output brightness at PWM2 level.
State 3, At offtime turn off output.
State 4, Stay off.

I assume you want it to only run this cycle once, resetting or turning the controller off then on with restart the process?
How fast is the rampup?

Tom.... :slight_smile:

sorry not the case
1 startup rampup to pwm1 over 5seconds
2 rampup to pwm2 over 5seconds
3 output pwm2 until off signal
4 wait for timer to start again

right now issue is code does not compile but when it worked steps 1 and 2 were complete, 3 was on constantly

Hi,
I think you might be better to start again, you have so many if... if else... and else... statements and you are nesting them, this is what makes you code hard to follow.

Does my explanation on post #14, state what you need to do?

Tom.. :slight_smile:

Hi,
What variable starts the next cycle?

You would be better to have two ramps and stop nesting your functions as well.

Tom... :slight_smile:

Hi,
What are;

long ontime1 = 15000;
long switchtime = 10000;
long turnon = 30000;

How do you start the cycle and restart the cycle.
What is the cycle time, cycle start to next cycle start?

Hint: Naming you pins like this helps a lot too;

int ledfwdPin= 11;
int ledrvsPin = 10;
int ledPin =  ledrvsPin;
int ledrlyPin = 12;

Not sure why you want to give the same pin two different names.

You have four time intervals;

  • startup rampup to pwm1 over 5seconds
  • rampup to pwm2 over 5seconds
  • output pwm2 until off signal
  • wait for timer to start again

So program in four stages, have a look at switch.. case..;

Tom.. :slight_smile:

TomGeorge:
Hi,
What are;

long ontime1 = 15000;
long switchtime = 10000;
long turnon = 30000;

How do you start the cycle and restart the cycle.
What is the cycle time, cycle start to next cycle start?

Hint: Naming you pins like this helps a lot too;

int ledfwdPin= 11;

int ledrvsPin = 10;
int ledPin =  ledrvsPin;
int ledrlyPin = 12;




Not sure why you want to give the same pin two different names.

You have four time intervals;

- startup rampup to pwm1 over 5seconds
- rampup to pwm2 over 5seconds
- output pwm2 until off signal
- wait for timer to start again


So program in four stages, have a look at switch.. case..;
https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/

Tom.. :)

hey tom, sorry sleep called so sorry for late reply

ontime is how long the pwm needs to be at the PWM2 for

switchtime is how long the accumulated switch time +rampup1 and rampup 2 needs to be before switching to led 10

turn on is there to say how long the total is. so ramp up 1, ramp up2 then ontime = 25 seconds so far. so for the other 5 seconds (30 seconds - 5) it is off. more a reference

at the moment the cycle does not start which is what i asking for help for
cycle time is labelled (ontime1)

pins are labelled, you literally just put pin after each.... i did not but it does the same thing.

the next question i think you might be asking why i wrote led = ledrvs. this is so later i can turn it to ledfwd and vice versa without rewriting the whole code and finding the variances, THE RAMPUP WORKS as a standalone, just the rest giving me issues now.

lastly there is a 5th timer that counts how long your steps 1,2,3 take and compare to variable "switchtime". if at the end of cycle 1,2,3 the total is >= swtichtime then led = ledrvs, then next time it is >= switchtime led = ledfwd.

hope this helps answer some question.

as for switch case, if else works..... if else also works in this case.