Hi, i'm working on a project with my car and currently i'm faceing a little problem:
In the loop i have multiple things, for example, scan for a RFID card, check a vibration sensor and i also want to keep an PWM signal without any interuptions depending on a button:
PWM will continue to "run" until it's stopped so there's no reason to re-start it every time through the loop.
You might want to set-up an AND condition with a flag that keeps track of if the PWM is running or not. That way you can start PWM only when your usaPin is high and PWM isn't already running.
There is a very small chance that analogRead() returns HIGH. analogRead() returns a value between 0 and 1023 (both included). HIGH is defined as 1; so only if analogRead() returns 1, you will set the PWM output to 120.
I do not understand what you try to achieve with a capacitor? I also do not quite understand 'toggler with resistance'.
Maybe you can draw a little schematic for both scenarios (photo/scan of hand-drawn one will do).
Analog pins can be used as digital pins; just use digitalRead().
And I do not understand the rest of the code; you use a variable usaStatus whose value never changes. If you want to detect a statechange, have a look at the statechange dectection example that comes with the IDE.
And why the single '=' in * if (usaStatus = 0) {* ?
int usaStatus = 0;
void loop()
{
delay(100);
if (digitalRead(usaPin) == HIGH) { //if toggler is ON
if (usaStatus == 0) {
analogWrite(usaoutPin, map(usalight, 0, 100, 0, 255)); //set PWM of usa lights
usaStatus = 1;
}
}
else //if toggler is off
{
if (usaStatus == 1) {
analogWrite(usaoutPin, 0); //set off usa lights
usaStatus = 0;
}
}