I'm trying to work out where I have gone wrong here,
I am having problems redefining variables for use in delay() function the problem is that the variable in this case 'Nate' will only redefine if its the default action in an if statement so in the code below it will stay the same value even when any of the designated button states are high meaning whatever I press the LED only comes on for the 4 seconds it was defined as initially, However in the part I've labeled Numbering where the if else statement is if I add something like " else {Nate = basicArray[1]} " then whatever button is pressed it will now run delay the LED on for 1 second
I have tested it by having it turn different LEDs on depending which button is pressed and that works so I am pretty sure its not that the buttons are defective or improperly wired
Either way I'm pretty stumped,
Many thanks,
Nate
const int switch2 = 2;
const int switch4 = 3;
const int switch8 = 4;
const int switch16 = 5;
const int relayPin = 9;
const int triggerPin = 15;
int buttonState2 = 0;
int buttonState4 = 0;
int buttonState8 = 0;
int buttonState16 = 0;
int buttonStateTrig = 0;
void setup() {
pinMode(switch2, INPUT);
pinMode(switch4, INPUT);
pinMode(switch8, INPUT);
pinMode(switch16, INPUT);
pinMode(relayPin, OUTPUT);
}
void loop() {
buttonState2 = digitalRead(switch2);
buttonState4 = digitalRead(switch4);
buttonState8 = digitalRead(switch8);
buttonState16 = digitalRead(switch16);
buttonStateTrig = digitalRead(triggerPin);
float basicArray[4] = {500, 1000, 8000, 4000};
float Nate = basicArray[3];
//Numbering
if(buttonState2 == HIGH)
{Nate = basicArray[0];}
else if (buttonState4 == HIGH)
{Nate = basicArray[1];}
else if (buttonState8 == HIGH)
{Nate = basicArray[2];}
else if (buttonState16 == HIGH)
{Nate = (basicArray[3]);}
// Trigger
if(buttonStateTrig == HIGH)
{(digitalWrite(relayPin, HIGH));
(delay(Nate));}
else
{digitalWrite(relayPin, LOW);}
}
Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool. I recommend you to use the standard IDE instead.
Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason or random blank lines just make for more scrolling when we're trying to read your code.
A very valuable troubleshooting technique when you encounter a problem like this is to add a bunch of Serial.print() statements throughout the code so that you can actually see the value of the various variables and which parts of your code are running printed to the Serial monitor.
unsigned long basicArray[4] = {500, 1000, 8000, 4000};
unsigned long Nate = basicArray[3];
because delay() does not expect a float
Because of the way you have defined your variables the value of Nate is going to be set to array[3] every iteration of loop() and that will probably happen thousands of times per second. If you want it to remember the value that came from the various IF statements you need
static unsigned long Nate = basicArray[3];
What sort of switches are you using and how are they wired? Is it possible that your inputs are floating when the switch is open?
Put in some Serial.print() statements so you can see what the actual buttonStates are.
I set up a serial print which was a good idea, thank you!
It showed that the buttons do work fine
Next I added an "else" to save from the "dangling else" as per KeithRB's suggestion, again thank you!!
and finally I did change from floats to unsigned longs and it works as the code would however for my purpose I need the variable to be to at least 2dp as it's going to an F Stop timer for making photographic prints
is there any way I can use the delay function or something similar (I've had a brief look at milis() but not enough to wrap my head around) but still have the time the light is on to be something other than just integers (this is why I didn't initially define them with "int")
just know that "5000" is 5.000 seconds. You can even enter it that way and have your input routine convert it to an unsigned long, i.e., enter 5.23 and have the computer multiply by 1000 to turn it into 5230, which is what Delay() expects.
This is called "fixed point arithmetic" and is what is preferred for arduinos.