Been trying to create both an automatic and a manually toggleable code to be used in a lamp. The automatic being from the delay timings and the manual being through a button. However, the code in not working as expected. It is able to switch from the 1st set of values to the 2nd occasionally, as if by random. Nothing should be wrong with the circuitry. Help is greatly appreciated. Thanks
int pin1 = 12;
int pin2 = 11;
int pin3 = 10;
int pin4 = 9;
int pin5 = 8;
int readVal;
float timE;
unsigned long dt1 = 10000;
void setup() {
// put your setup code here, to run once:
pinMode(A0, INPUT);
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pin5, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
readVal = digitalRead(A0);
timE = 9;
Serial.println(timE);
Serial.println(readVal);
if (timE == 9) {
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
digitalWrite(pin5, HIGH);
if (readVal == 0) {
Serial.println(readVal);
timE = timE + 0.5;
}
} else {
delay(dt1);
timE = timE + 0.5;
}
if (timE == 9.5) {
Serial.println(timE);
Serial.println(readVal);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, HIGH);
digitalWrite(pin5, LOW);
if (readVal == 0) {
Serial.println(readVal);
timE = timE - 0.5;
}
} else {
delay(dt1);
timE = timE - 0.5;
}
}
It is good to have confidence like that, but it would be better to post a schematic, hand drawn and take a photo of it to upload is actually the easiest and best way.
Show all the parts and how they are connected, with care to show where the power is coming from and exactly how any buttons and the like are wired.
We can look at you code in the meantime, but it would really help to have a diagram.Which you shoukd have in front of you anyway, it's as good for you to have as it will be for us.
TIA and welcome to the forum.
This right away
if (timE == 9.5) {
may never be true. Testing floating point numbers for exact equality is problematic, better to test greater or less than, or use absolute value to see if a floating point number is "close enough" to a constant.
My experience has shown that when putting an int into a float; such as float mything=0; the type declaration can be changed to an int with code optimization. Better safe.
The task can easily be realised with an object.
A structured array contains all the information, such as the pin addresses for the I/O devices, as well as the information for the timing.
A single service takes care of this information and initiates the intended action.
The structured array makes the sketch scalable until all I/O pins are used up without having to adapt the code for the service.
It is cool stuff, isn´t it?
Hi, sorry about the circuitry thing, will remember to post a picture of it too in future posts, attached is a similar circuit diagram, just with different pins and a Mega 2560 as opposed to an uno
I don't doubt your experience, I do question your interpretation of what actually happened.
For the compiler to optimise by changing the type of a variable, to be "correct" in any way it would have to mean it did not matter.
Therefor, how did you even know?
Until you can provide a complete sketch that illustrates the problem, I will assume you have made a mistaken conclusion.
It is good to use explicitly floating constants, as a matter of style, but it is not necessary in the specific case you site.
And if you can post such an example, it should immediately be forwarded to wherever they deal with compiler errors, as that would certainly be one, for real.
As you may have seen, compiler errors are both very rare and too often blamed for behaviours that have perfectly adequate explanations.