Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
lukatopgun
Published © LGPL

Quick example of flags with a servo

Servo + fan will turn on when pressed button

AdvancedFull instructions provided7,490
Quick example of flags with a servo

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
DC Motor, 12 V
DC Motor, 12 V
Maybe not nececarily 12V
×1
Female/Female Jumper Wires
Female/Female Jumper Wires
×3
Solderless Breadboard Full Size
Solderless Breadboard Full Size
Not nececary
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Circuit diagram

The pins may not be 100% right

Code

Code

C/C++
The code with correct pins
#include <Servo.h>

int val; // Define variable val
int button1 = 4; // Define the button in D4
Servo servo1; //name servo

bool myFlag = false; //create + name flag

void setup() {
  pinMode (button1, INPUT); // The button interface is defined as output
  pinMode (7, OUTPUT); //define D7 pin as output so it is forward spinning of a motor
  pinMode (6, OUTPUT); //define  D6 pin as output so it is backward spinning of a motor
  servo1.attach(9); //locate servo

}

void loop() {

  if (myFlag == false) //if myFlag is true turn off

    for (int i = 0; i < 180; i++) {

      val = digitalRead (button1); // Read the digital 4 level value and assign it to val
  if (val == LOW) // Whether the key is pressed, the light will be on when pressed
  {
    digitalWrite (7, HIGH);
    servo1.write(i); //servo will spin randomly
    myFlag = true; // set the flag to true thus turning servo1 off
    delay(100); // 100ms cooldown time
    myFlag = false; // set the flag to false thus turning servo 1 on
  }
  else
  {
    digitalWrite (7, LOW); //if buton not pressed fan turns off
  }


}
}

Credits

lukatopgun
2 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.