Im building my first project because i needed a timer to press a button in once every 7 days.
The issue i run in to is that I get a false trigger when starting up. (specificly when i switch the PINMODE from Pin D7 to OUTPUT)
As i understand this is often the case with a Arduino Every in start up having to do with floating voltages and can be solved either by soldering in an resistance between the Output pin "D7"and the GND, or by using an internal resistance by programming.
Ive spend time searching for the right lines of codes but so far are not able to get it to work.
Due to my lack of experiance i might be doing something completly wrong, but i hope someone can at least point me in the right direction
const int devicePin = 7;
// Weekly interval in milliseconds: 1000ms * 60s * 60m * 24h * 7d
unsigned long previousMillis = 0;
unsigned long interval = 604800000UL;
void setup() {
digitalWrite(devicePin, LOW);
pinMode(devicePin, INPUT_PULLUP);
pinMode(devicePin, OUTPUT);
digitalWrite(devicePin, LOW);
previousMillis = millis();
delay(3000);
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
digitalWrite(devicePin, HIGH);
previousMillis = currentMillis;
}
else
{
digitalWrite(devicePin, LOW);
}
}
I presume you are driving either a relay or mosfet to 'actuate the button'. Please show us your output circuit. A photo of a pencil sketch will do.
Your loop() code will result in a VERY short pulse each time the time criteria are met; As @qubits-us says, you should extend that, as it may not result in a pulse long enough for the system you are trying to trigger.
When the Every starts up, your pin will be an INPUT and be floating.
When your code writes a LOW to it... I don't think anything will happen. Your pin will likely still be floating. You could write a test sketch and check it with a meter.
When your code configures the pin as INPUT_PULLUP, it will be weakly pulled to 5V.
When your code configures the pin as OUTPUT, it will be set LOW (unlike in the 328P, where setting to pin to INPUT_PULLUP prior to setting it to OUTPUT would result in a HIGH).
Whether or not that's what you want to happen is something only you know. But that's what your code is telling the processor to do.
If your undescribed button is triggering on the weak pullup or a floating input, I have to wonder what it's hooked up to.
from the tech doc, it's evident your external button is connected to pins labeled 0V and IN, and a spst button is used to short the two. So, you're looking to provide a short pulse to 0V.
your code is structured to achieve the opposite - a short high pulse at the end of the time period, then low.
This is the equivalent of holding the button all the time, releasing and pressing again to get the unit to operate.
At least, that's all I can determine from what you've provided. If I'm correct, then your code should establish a high at startup, keeping the pin high all the time until you provide a short low pulse.
Good luck!
Apparently you need to start with pin D7 (PA1) high, and after interval pulse it low. Unfortunately, Arduino header files for Nano Every contain a flaw, making this impossible using Arduino Language. Direct register access can be used to work around.
In setup():
VPORTA.OUT |= 0b00000010; // to set PA1 high
VPORTA.DIR |= 0b00000010; // to make PA1`output
In loop() after if-statement:
VPORTA.OUT &= ~0b00000010; // to set PA1 low
delayMicroseconds(5); // to add a delay equivalent to digitalWrite
VPORTA.OUT |= 0b00000010; // to set PA1 high
To understand the meaning of VPORT you might read the datasheet.
It's been discussed in this forum many times before.
You might also read:
C:\Users\USER\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.8\cores\arduino\wiring_digital.c
Did you read my last post to you? (not the useless divergence with @stitech). You must determine whether the system triggers on a high-low, or a low-high. By definition, even if you manage to keep the input high from power on forwards, if low-high triggers the system, so will power-off to power-on.
You might be better off using a relay to drive the button input, as at least then, you might be able to prevent the relay from clicking at power-on, and only happen on timeout.