Help implementing analogwrite into my code

Hey guys.

So me & a friend have been working on this code.

its an LED sequencer.

The Red running light will fade on using analogwrite and I can use the “max ramp value” to set a brightness level (lower number = dimmer led)

however I am struggling to implement the analog write function into the amber so that I can control the brightness of the amber LED too.

Would any one be able to help, it would be highly appreciated.

Thanks

#include <ESP8266WiFi.h>

// ------- pin assignments -----------
const byte Amber1 = 14; // Amber output switch
const byte Amber2 = 12;
const byte Amber3 = 13;
const byte Amber4 = 15;
const byte Reds = 4; // Red Output switch (group of 3 LED sets)
const byte Reds2 = 16; // Red Output switch (single output cluster)
const byte Indicator = 5; // Indicator input pin
// ------------ Change to re-configure the boards characteristics -----------
const boolean IndicatorOn = LOW; // state of the indicator input when ON
const boolean RelayOn = HIGH; // active level state for switching output ON
const unsigned int IndicatorInterval = 70; // mS delay between amber step transitions
unsigned int RedsInterval = 2000; // mS delay before RED leds are switched back on after indicator released
const unsigned int RampIncrement = 10; // determines the Fade ramp up speed of Reds when ON - increase to slow down the rampconst boolean RelayOff = !RelayOn;
// ---------------------- Program variables and flags -----------------------
const boolean IndicatorOff = !IndicatorOn;
const boolean RelayOff = !RelayOn;
unsigned int IndicatorRunningTimer;
unsigned int RedsRunningTimer;
boolean IndicatorStatus;
boolean RedsDelay;
byte AmberPointer;
boolean PreviousIndicatorState;
boolean FirstPass;
boolean RedRamp;
unsigned int RampValue;
// -----------------------------------------------------------------------------
void setup()
{
pinMode(Indicator,INPUT_PULLUP);
PreviousIndicatorState = digitalRead(Indicator);
if(PreviousIndicatorState == IndicatorOff)
{ // indicator is OFF at start up so switch Reds on
RedRamp = true;
RampValue = 0;
FirstPass = false;
pinMode(Reds,OUTPUT);
pinMode(Reds2,OUTPUT);
}
else
{
FirstPass = true; // indicator is on at start up so enable an Indicator sequence
RedRamp = false;
}
WiFi.disconnect(); // turn off WiFi radio section (reduce current consumption)
WiFi.mode(WIFI_OFF); // turn off WiFi radio section (reduce current consumption)
WiFi.forceSleepBegin(); // turn off WiFi radio section (reduce current consumption)
Serial.begin(115200); // Serial port for debugging
Serial.println("");
Serial.println(“Sequencer 3V0”);
Serial.println("");
pinMode(Indicator,FUNCTION_3); // reassign RX pin to be a normal GPIO
pinMode(Indicator,INPUT_PULLUP);
AmberPointer = 0; // reset Amber sequencer to amber 1 LED
RedsDelay = false; // disable Reds delay timer
IndicatorStatus = false;
IndicatorRunningTimer = millis();
RedsRunningTimer = millis();
} // end of void Setup()
// ---------------------------------------------------------------------
void loop()
{
if(digitalRead(Indicator) == IndicatorOn && (PreviousIndicatorState == IndicatorOff || FirstPass == true))
{ // Indicator just switched on
Serial.println(“Indicator >> On”);
FirstPass = false;
PreviousIndicatorState = IndicatorOn;
IndicatorStatus = true; // enable Indicator sequence
RedsDelay = false;
digitalWrite(Reds,RelayOff);
digitalWrite(Reds2,RelayOff);
Serial.println(“Reds >> Off”);
AmberPointer = 0;
IndicatorRunningTimer = millis();
}
else if(digitalRead(Indicator) == IndicatorOff && PreviousIndicatorState == IndicatorOn)
{ // Indicator just Switched off
PreviousIndicatorState = IndicatorOff;
IndicatorStatus = false;
digitalWrite(Amber1, RelayOff);
digitalWrite(Amber2, RelayOff);
digitalWrite(Amber3, RelayOff);
digitalWrite(Amber4, RelayOff);
Serial.println(“Indicator >> Off”);
RedsDelay = true; // enable Red delay timer
RedRamp = false;
RedsRunningTimer = millis();
}
delay(10);
// ------------------------------------------------------------
if(IndicatorStatus == true) // if indicator has been switched on
{
unsigned int IndicatorTimer = millis() - IndicatorRunningTimer;
if (IndicatorTimer > IndicatorInterval) // timer overflow
{ // sequencer step
IndicatorRunningTimer = millis();
if(AmberPointer < 4)
{
AmberSequencer(); // Amber sequence timeout - switch amber leds
AmberPointer ++;
} // end of if(AmberPointer < 4)
} // end of if (IndicatorTimer > IndicatorInterval)
} // end of if(IndicatorStatus == true)
// --------------------------------------------------------------
if(AmberPointer == 4) IndicatorStatus = false; // end of indicator sequence
if(RedsDelay == true)
{
unsigned int RedsTimer = millis() - RedsRunningTimer;
if (RedsTimer > RedsInterval) // timer overflow
{
RedsRunningTimer = millis();
RedsDelay = false;
pinMode(Reds,OUTPUT);
pinMode(Reds2,OUTPUT);
//digitalWrite(Reds, RelayOn);
RedRamp = true; // enable red to be able to ramped
RampValue = 0;
Serial.println(“Reds ON”);
}
} // end of if(RedDelay == true)
// ------- Ramp Red value brightness if enabled ----------------
if(RedRamp == true)
{
analogWrite(Reds,RampValue);
analogWrite(Reds2,RampValue);
//Serial.println(RampValue);
RampValue = RampValue + RampIncrement; // increment brigntness value
if(RampValue >= 200) RedRamp = false; // if brightness > 100%, stop ramp
}
} // end of void loop()
// -----------------------------------------------------
void AmberSequencer()
{
switch (AmberPointer)
{
case 0:
Serial.println(“Amber 1 ON”);
pinMode(Amber1, OUTPUT);
digitalWrite(Amber1, RelayOn);
break;
case 1:
Serial.println(“Amber 2 ON”);
pinMode(Amber2, OUTPUT);
digitalWrite(Amber2, RelayOn);
break;
case 2:
Serial.println(“Amber 3 ON”);
pinMode(Amber3, OUTPUT);
digitalWrite(Amber3, RelayOn);
break;
case 3:
Serial.println(“Amber 4 ON”);
pinMode(Amber4, OUTPUT);
digitalWrite(Amber4, RelayOn);
break;
} // end of switch (AmberPointer)
} // end of void AmberSequencer()
// ----------------------------------------------------------------

Craig

So no schematic and the code posted incorrectly, not the best start. I am sure that after 11 posts some on should have advised you to read the how to use this forum sticky post. It tells you about asking questions.

I am assuming that you haven’t got a proper Arduino but a ESP8266. Have you checked what pins are capable of PWM output?