Starting first project, want someone to double check my code

Hey all.

I just recently started up with Arduino and went through the tutorial for this and I wanted to finally make a project with it, but I'm not sure if I got the code right.

The idea is to have two 3" fans for next to my bed, for the nights when it's too hot to not have a fan on, but too cool to have the overhead fan on.

Even if I pull it apart later, I just wanna start small, you know?

The code I have goes as this (I'm only trying to turn both of them on or off, no changing of speeds yet; baby steps...)

const int fan_1 = 11;
const int fan_2 = 9;

const int button = 2;

int state = 0;
int val = 0;
int old_val = 0;

void setup(){
  pinMode(fan_1, OUTPUT);
  pinMode(fan_2, OUTPUT);
  pinMode(button, INPUT);
}



void loop (){
  val = digitalRead(button);
  if((val == HIGH) && (old_val == LOW)){
    state = 1 - state;
  delay(10);
}
    
    old_val = val;
    
  if(val == HIGH){
    
   digitalWrite(fan_1, HIGH)}
   else{
   digitalWrite(fan_1, LOW)}

if(val == HIGH){
  digitalWrite(fan_2, HIGH)
}else{
  digitalWrite(fan_2, LOW)}
}

Will that work?

Will that work?

You need to learn about the Tools + Auto Format option. You should put all the { on new lines, first.

Why are there two if(val == HIGH) statements?

What is actually connected to pins 9 and 11? Not the fans themselves, I hope. Electric motors draw way too much current to be powered by an Arduino pin.

if(val == HIGH){
    
   digitalWrite(fan_1, HIGH)}
   else{
   digitalWrite(fan_1, LOW)}

aka

digital write (fan_1, val);

consider changing it slightly like this, which uses your state change (good work there) but is only changing the state of the 'fans' when the button is pressed)

const int fan_1 = 11;
const int fan_2 = 9;
const int button = 2;
int old_val = 0;
int state = 0;

void setup()
{
  pinMode(fan_1, OUTPUT);
  pinMode(fan_2, OUTPUT);
  pinMode(button, INPUT);
}

void loop ()
{
  int val = digitalRead(button);
  delay(50);//debounce
  if(val == HIGH && old_val == LOW)
  {
    state = !state;
    digitalWrite(fan_1, state);
    digitalWrite(fan_2, state);
  }
  old_val = val;
}
    state == !state;

Will always be false. But, so what? It's a rather useless test. Nothing happens either way.

PaulS:

Will that work?

You need to learn about the Tools + Auto Format option. You should put all the { on new lines, first.

Why are there two if(val == HIGH) statements?

What is actually connected to pins 9 and 11? Not the fans themselves, I hope. Electric motors draw way too much current to be powered by an Arduino pin.

Was gonna connect them via breadboard and resistors, is that too much current? (I haven't yet figured out which resistors I need, but will do that tomorrow before I buy the fans).

@AWOL: thanks :smiley: the tutorials didn't include shortcuts like that.

PaulS:

    state == !state;

Will always be false. But, so what? It's a rather useless test. Nothing happens either way.

ok, bully for you... you found a typo

corrected above