Nano Every programming question

I'm trying to run a vent flap actuator with a forward/reverse module through a nano every. I've only used the standard nano before, I accidentally ordered a nano every, so here we go.

It's a simple on/off toggle, meant to operate as: on-flapdown, off-flapup. It's working once, and then not allowing more switch inputs. Once I flip the switch, it follows the correct input from the switch. But just one time. I've tried internal and external pull-ups for the input pin 2, 10k external though not 7.4, if that matters?

I've made a similar unit before with a standard nano, and it worked fine. I must be missing something!

/*
 Vent Controller with AC
*/

#include <Servo.h>

int ac = 2;     // 0v to pin opens condenser flaps
int openflap = 10;
int closeflap = 9;
int acval;

void setup() {
 
  pinMode(ac, INPUT);
  pinMode(openflap, OUTPUT);
  pinMode(closeflap, OUTPUT);
  digitalWrite(openflap, HIGH);
  digitalWrite(closeflap, HIGH);
}

void loop() {
   
  acval = analogRead(ac);            
 
  if (digitalRead(acval) == LOW) {
    digitalWrite(openflap, LOW);
    digitalWrite(closeflap, HIGH);
    }

  else {    
    digitalWrite(openflap, HIGH);
    digitalWrite(closeflap, LOW);
    }
  
  delay(10);  
}

So you read an analogue input and use it to determine the pin to read; just consider what happens if acval equals e.g. 500? You're code will read pin 500 (probably 500 - 256); does that pin exists?

like this

const int Thresh = 500;         // guess
void loop()
{
    if (Thresh < analogRead(ac)) {
        digitalWrite(openflap, LOW);
        digitalWrite(closeflap, HIGH);
    }

    else {
        digitalWrite(openflap, HIGH);
        digitalWrite(closeflap, LOW);
    }

    delay(10);
}

I'm clearly not a great programmer lol... I'm just trying to sense a ground input on A2, when ground input is present I want it to trigger D10 to low, and when it's not present I want D9 to go low and 10 to switch high. I think my program might be overcomplicated?

What is the thresh=500 function?

How would acval = 500? My intent was just to read pin 2 as either hi/lo?

Because you read it as an analog voltage, viz:

  acval = analogRead(ac);            

then use it as if it is digital. @someone made the assumption you meant to be testing an analog value for being above, or below, some threshold and used a random value between the extremes possible, 0 to 1023 the range of analogEead().

If it is a digital value or sensor on the pin, use digitslRead(), which will indeed return either HIGH or LOW.

Or make it clear what is on the pin, why you used analaogRead().

You might have

  acval = (analogRead(ac) > 500) ? HIGH : LOW;

to do the conversion. Direct use of the analog value like you have is not sensible.

a7

I tried editing the code to be simpler, and use digitalread, still no dice-

/*
 Vent Controller with AC
*/

#include <Servo.h>

int ac = 2;  // 0v to pin opens condenser flaps
int openflap = 10;
int closeflap = 9;

void setup() {

  Serial.begin(9600);
  pinMode(ac, INPUT);
  pinMode(openflap, OUTPUT);
  pinMode(closeflap, OUTPUT);
  digitalWrite(openflap, HIGH);
  digitalWrite(closeflap, HIGH);
}

void loop(){

  

    if (digitalRead(ac) == LOW) {
        digitalWrite(openflap, LOW);
        digitalWrite(closeflap, HIGH);
    }

    else {
        digitalWrite(openflap, HIGH);
        digitalWrite(closeflap, LOW);
    }

    delay(10);
}

BTW, Always show us a good schematic of your proposed circuit.

Show us good images of your ‘actual’ wiring.

Give links to components.

buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

a button press can be recognized by detecting a change in state and becoming LOW

I've tried internal pullup, but right now I've got a 10k external pullup resistor installed from 5v to the A2 pin. Could this be something to do with the Nano Every? I've not had this problem with the standard Nano

Show us good images of your ‘actual’ wiring.

an external pull-up should just as well, with or without the internal pull-up enabled

but pin 2, actually D2 is not the same as A2

The terminal blocks and 5v regulator aren't being used for this, it's just a little test rig that I use for arduino projects of different types. The switch on the right switches ground from GND pin to pin 4, with an external 10k pullup. The fwd/reverse relay module just looks for ground on the blue/green wires to trigger forward or reverse.

/*
 Vent Controller with AC
*/

#include <Servo.h>

int ac = 4;  // 0v to pin opens condenser flaps
int openflap = 10;
int closeflap = 9;

void setup() {

  Serial.begin(9600);
  pinMode(ac, INPUT);
  pinMode(openflap, OUTPUT);
  pinMode(closeflap, OUTPUT);
  digitalWrite(openflap, HIGH);
  digitalWrite(closeflap, HIGH);
}

void loop(){

  

    if (digitalRead(ac == LOW)) {
        digitalWrite(openflap, LOW);
        digitalWrite(closeflap, HIGH);
    }

    else {
        digitalWrite(openflap, HIGH);
        digitalWrite(closeflap, LOW);
    }

    delay(10);
}

You have:


int ac = 2; 

However, you are wired to A2 A4.

:pleading_face:


Edit

if (digitalRead(ac == LOW))

Change to:

if (digitalRead(ac) == LOW)

Also, Are you now using D4 or are you using D2 or are you using A2 ?

Oh wow, I actually switched it to a4, thinking maybe 2 wasn’t working. I copied the sketch before I updated it. Do I have to call the pin a4 instead of 4? I haven’t had to do that in the past?

if (digitalRead(ac == LOW))

Change to:

if (digitalRead(ac) == LOW)

Also, Are you now using D4 or are you using D2 or are you using A2 or are you using A4 ?

Try it, you will like it. :sunglasses:

const byte ac = A4;

. . .

if (digitalRead(ac) == LOW)

What's the const byte function do?

After testing with a multimeter, the switch input is registering properly to the arduino. The output pins are reading .6ohm to ground on D9 and nothing on D10. When I flip the switch, D9 goes to no contact, and D10 is .6ohm. It's not enough connection to ground for the fwd/rev relay module to see it though, what could cause this?

Ohms? Are you using your ohmmeter on live circuit?

Try again all experiments and look at voltages on the pins.

a7