Loading...
Pages: [1]   Go Down
Author Topic: Iron man helmet faceplate and LED  (Read 805 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys i'm new to the Arduino and i have a Arduino Uno and i don't know how to write the code.
Can some pro please write me a simple code which turn 180 to open up the faceplate and LED turn off with a press of a button, then if press again it will turn back 180 counter clock wide to close the faceplate then turn on the LED.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 96
Posts: 6371
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const int servoPin = 2;
const int buttonPin = 3;
const int LEDPin = 4;

#include <Servo.h>

Servo visorServo;

void setup() {
  visorServo.write(0);  // Initial position
  visorServo.attach(servoPin);

  pinMode(buttonPin, INPUT_PULLUP);  // Connect button between pin and GROUND. LOW when pushed.

  pinMode(LEDPin, OUTPUT);
  digitalWrite(LEDPin, LOW);  // LED off
}

void loop() {
  static unsigned long lastPushedTime = 0;
  static boolean visorClosed = true;
  static boolean lastButtonState = HIGH;
 
  boolean newButtonState = digitalRead(buttonPin);

  // If the button is down and it has been a while since it was last down...
  if (newButtonState  == LOW && lastButtonState == HIGH && millis() - lastPushedTime > 100) {
    lastPushedTime = millis();
    if (visorClosed) {
      visorServo.write(180); // Open visor
      visorClosed = false;
      digitalWrite(LEDPin, HIGH); // Turn on light
    }
    else {  // Viso is open
      visorServo.write(0); // Close visor
      visorClosed = true;
      digitalWrite(LEDPin, LOW); // Turn off light
    }
  }
 
  lastButtonState = newButtonState;
}
« Last Edit: January 11, 2013, 06:32:57 pm by johnwasser » Logged

UK
Offline Offline
Tesla Member
***
Karma: 89
Posts: 6386
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Turn *what* 180 degrees?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 96
Posts: 6371
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Turn *what* 180 degrees?
I'm guessing a servo.
Logged

Pages: [1]   Go Up
Print
 
Jump to: