Set pin 7 to output but nothing happens

I am trying to make a solenoid control for a maple sap tank. The tank is under vaccum to draw the sap into it like a straw. I have two float switches inside to control when a solenoid valve opens to allow air in to drain the tank and to close the valve to return the tank to vaccum. I am using a Alamscn mini Nano v3.0 with a ATmega328p chip, screw down base for the Nano, and a one relay module. it is powered by a 12v, 5A power supply. I would like to have the Nano veiw the switches as momentary push buttons, and I cannot see why that is not possible, both are just switches. The program I wrote compiles good and uploads just fine. The inputs seem to work like they are supposed too (pin 4 bottom switch, pin 5 top switch), but the output (pin 7) does not send any signal to the relay to open and close it. Always read 0v. What i got is below.

#import <Arduino.h>
//Float Switch
int top = 5; //Pin Reed
int bottom = 4;//Pin Reed
int relay = 7; //relaysignal

void setup() { //code that only runs once
// set pin modes
pinMode(top, INPUT_PULLUP);//setup top float switch
pinMode(bottom, INPUT_PULLUP);//setup bottom float switch
pinMode(relay, OUTPUT);//setup relay trigger
Serial.begin(9600);//print to serial
}

// set switch state
int topstate = digitalRead(top); // set hls switch
int bottomstate = digitalRead(bottom); //set lls switch

void loop(){

//compare hls state has changed, open solenoid
if (top != topstate) {
  // if the state has changed, turn on solenoid
  if (top == HIGH) {
    // if the current state is high then switch went from on to off
    digitalWrite(relay, HIGH);
  }
  // little delay to avoid bouncing
    delay(500);
    }

// save current hls state as last state, for next time through the loop
int topstate = digitalRead(top);

//compare lls state has changed, close solenoid
if (bottom != bottomstate){
  // if the state has changed, turn off solenoid
  if (bottom = LOW);{
    // if the current state is high then button went from off to on
     digitalWrite(relay, LOW);
  }
  
  delay(500);
}

// save current lls state as last state, for next time through the loop
int bottomstate = digitalRead(bottom);
}

Any help would be nice, thanks

Rob, Backyard Syrup Co.

Without a schematic, it's hard to say what might be going on.

1 Like
 if (bottom = LOW);

There are 2 things wrong with this line of code

  • bottom is being set to LOW not compared to LOW
  • the semicolon should not be there

Other then that, it should work correct?

tried second board, it also has nothing coming off pin 7.

In a number of places you check whether top is HIGH or LOW but it will always be HIGH because you set its value to 5

i set the checks to digitalRead and that seem to work, thanks for the help

That does sound better

Looking at the code that has yet to be posted I do not see where you are defining the inputs as pull up. Then when the switch is pressed you will get a low if the switch is not pressed you will get a high.

What am I missing? Did you post in the wrong topic?

Me too. The code was posted 5 hours before @gilshultz 's question and has not been amended. The INPUT_PULLUPs are right there.

Hi @backyardsyrup

I'm curious why your code is debouncing the buttons? I can't immediately see any need to do that.

Perhaps the circuit or code will become more complex in future. Right now, it's so simple that you could just use an SR flip-flop which you can easily make with a 74hc00 chip or similar.

But an Arduino looks much better than a simple SN7400 plus capacitors. :sunglasses:

I know you are making a little joke @paulpaulson .

The Arduino would look "better" to someone with no electronics experience because, in their eyes, using an Arduino is more "cool".

But to someone with expertise in electronics, they would think it looks stupid and wasteful because a microcontroller/Arduino is being used where a simple SR flip flop could do the same job.

But hopefully @backyardsyrup will say this is only the beginning and they want to add features X, Y and Z in future, and that will justify using a microcontroller/Arduino.

1 Like
  1. new to this
  2. was not looking to "create" board
  3. Nano board already together
  4. size does matter in this case
  5. and yes I do like how the Nano board looks
    I know this is considered fairly simple, but that is what I was going for. It is actually for sap level in a tank with float switches and not buttons. I did not want to be in the woods trying to figure out what is wrong. I tested my set-up and it works the way it should, and I am happy with that. And who knows, maybe I will add more to it later, but for now, it will do its job happily.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.