I've spent 3 hours in my boxer shorts scratching my head over something that I know must be simple fix and now I'm going to ask for help :-[
I've been editing existing code which has taught me a lot. Originally is was on an infinite loop (which I don't want as I just want a single instance). But since I've deleted that loop code, I now can't get the program to start. Goal: LED fades bright and delivers a single pulse and turns off only when I connect it to power.
Here's my progress...
// rampUp allocations
const int ledPin = 3; // LED #1 on pin 3 (PWM)
boolean activate = false; // ramp up not active
unsigned long currentTime; // for tracking time
unsigned long loopTime; // during ramp up
int brightness = 0; // brightness during ramp up
int fadeAmount = 1; // increments to ramp up by
// flashLed allocations
const int interval = 150; // 150ms flash time
static unsigned long previousMillis; // flash time counter
static boolean firstRun = true; // start of flash process
void setup ()
{
pinMode (ledPin, OUTPUT);
} // end of setup
void flashLed ()
{
unsigned long currentMillis = millis();
if (firstRun == true) { // If start of flash
previousMillis = currentMillis; // advance timer
firstRun = false; // flash now in progress
}
if (currentMillis - previousMillis >= interval) { // If led has been HIGH for enough time
previousMillis = currentMillis; // advance timer
digitalWrite(ledPin, LOW); // switch off led
firstRun = true; // reset flash process indicator
brightness = 0; // reset ready for next rampUp
}
}
void rampUp ()
{
if (activate = true); { // ramp up underway
while (activate == true) {
currentTime = millis();
if (currentTime >= (loopTime + 20)) { // while rampup underway, increment time
analogWrite (ledPin, brightness); // write current brightness
brightness = (brightness + fadeAmount); // fadeup the led
if (brightness < 50)
activate = true; // still below 50?
else {
activate = false; // above 50 so reset flag and switch led on full
digitalWrite (ledPin, HIGH);
}
loopTime = currentTime; // update time
}
}
}
activate = false; // process complete so reset flag
}
Firstly look at what you do in setup() and loop(). Basically just set the pinMode() of ledPin(). No other functions are called, hence nothing will happen.
There are two likely looking functions named rampUp() and flashLed() which you could call from setup() but there are problems with them. For instance, the first line of rampUp() is
if (activate = true);
That semicolon should not be there and = is not the comparison operator, == is
More seriously, for a programming problem 3 hours is very short. Real programmers have stamina (and coffee).
...R
Truer statements have never been made! I will never forget spending 8 hours per day for 5 days, and probably many hours when I was supposed to be asleep, looking for a single bit that was set wrong in 6502 assembly for a communications front-end processor. Did find it!
I'm far from being called the honor of 'programmer' I'm a artist by profession that flunked maths and science. So this is the first time dealing with code. But I'm not afraid of a challenge Other than 2 basic tutorials to make a LED blink and power 12volt devices. This is the first complex (for me) code I've played with. So thank you for your patience.
PaulMurrayCbr:
Put this stuff in setup(), then. setup gets called once when the board is powered up.
This was the first thing I tried, but as the others have said. There's more to the code that needs sorting to tell the LED to start in the first place.
UKHeliBob:
Where do I start ?
Firstly look at what you do in setup() and loop(). Basically just set the pinMode() of ledPin(). No other functions are called, hence nothing will happen.
There are two likely looking functions named rampUp() and flashLed() which you could call from setup() but there are problems with them. For instance, the first line of rampUp() is
if (activate = true);
That semicolon should not be there and = is not the comparison operator, == is
Great. I have no idea what text I need to put into now as I thought popping in OUTPUT would make it start as below...
pinMode (ledPin, OUTPUT);
Can anyone recommend good Arduino code courses? As so far they're so basic it leads no knowledge about the 'simple' issues you've all pointed out. Bit like I'm told what bits of my French paragraph needs correcting, but not knowing enough French to know how to correct it.
sillyface:
Can anyone recommend good Arduino code courses?
What is your preferred learning style?
Some people like to learn by studying examples and there are plenty of example programs with the Arduino IDE. Arduinos are programmed using C++ and there is documentation for features you may not understand on the cplusplus website. There are also tutorials.
Other people like a classroom style of learning - starting at A and working onwards towards Z. I don't have any specific recommendations for that style.
If the Arduino is not going to do anything else while the ramp up and flash occurs then you can vastly simplify the program. For instance the rampUp() function could be as simple as
void rampUp ()
{
for (int level = 0; level <= 255; level+= fadeAmount)
{
analogWrite(ledPin, level);
delay(20);
}
}
Call it from setup() and it will only run once
The delay() function, whist often misused, has its place in the right circumstances.
sillyface:
This was the first thing I tried, but as the others have said. There's more to the code that needs sorting to tell the LED to start in the first place.
For anyone else that requires this function of the USS Enterprise going to warp (sudden flash). Here's the corrected code attached that I had someone help me out with.
However, I've come across my next challenge which I'm not sure to post on a separate subject thread or keep here as it's connected to the overall function. When I replace single LED(s) with a LED strip using 12 volts via a MOSFAT. When power is applied, the LED strip comes on instantly before going to it's lowest dim level. But I need it to not light up when power is applied until my code tells it to. I'm assuming this is a MOSFAT circuit issue, can anyone confirm as it does this with any code?
// rampUp allocations
const int ledPin = 3; // LED #1 on pin 3 (PWM)
boolean activate = false; // ramp up not active
unsigned long currentTime; // for tracking time
unsigned long loopTime; // during ramp up
int brightness = 0; // brightness during ramp up
int fadeAmount = 1; // increments to ramp up by
// flashLed allocations
const int interval = 150; // 150ms flash time
static unsigned long previousMillis; // flash time counter
bool firstRun = true;
void setup ()
{
pinMode (ledPin, OUTPUT);
delay(3000);
rampUp();
flashLed();
delay(100);
digitalWrite(ledPin, LOW);
} // end of setup
void flashLed ()
{
unsigned long currentMillis = millis();
if (firstRun == true) { // If start of flash
previousMillis = currentMillis; // advance timer
firstRun = false; // flash now in progress
}
if (currentMillis - previousMillis >= interval) { // If led has been HIGH for enough time
previousMillis = currentMillis; // advance timer
digitalWrite(ledPin, LOW); // switch off led
firstRun = true; // reset flash process indicator
brightness = 0; // reset ready for next rampUp
}
}
void rampUp ()
{
if (activate = true); { // ramp up underway
while (activate == true) {
currentTime = millis();
if (currentTime >= (loopTime + 20)) { // while rampup underway, increment time
analogWrite (ledPin, brightness); // write current brightness
brightness = (brightness + fadeAmount); // fadeup the led
if (brightness < 50)
activate = true; // still below 50?
else {
activate = false; // above 50 so reset flag and switch led on full
digitalWrite (ledPin, HIGH);
}
loopTime = currentTime; // update time
}
}
}
activate = false; // process complete so reset flag
}
void loop() {
} // end of loop
Why 'opp?'?. The code works exactly how I want it to.
I've also had help with the below issue. Turns out I had a faulty MOSFET, and also I didn't have it wired up correctly with resistors Super happy now.
However, I've come across my next challenge which I'm not sure to post on a separate subject thread or keep here as it's connected to the overall function. When I replace single LED(s) with a LED strip using 12 volts via a MOSFAT. When power is applied, the LED strip comes on instantly before going to it's lowest dim level. But I need it to not light up when power is applied until my code tells it to. I'm assuming this is a MOSFAT circuit issue, can anyone confirm as it does this with any code?