Monday, February 18, 2013

Random Arduino Fun

Here's what I came up with putzing with different code projects. As you hold the button down, the light will flicker and pulse, but when it's not pressed, you have manual control turning it up or down. Here's the code:


int ledPin = 6;

int buttonPin = 3;

int buttonPressed = 0;

int bright = 0;

int dir = 1;

int potPin = A0;

int newValue = 0;

int potValue = 0;

void setup(){
  Serial.begin (9600);

  pinMode (ledPin, OUTPUT);

  pinMode (buttonPin, INPUT);

  pinMode (potPin, INPUT);

  digitalWrite (buttonPin, HIGH);
}

void loop (){

  buttonPressed = digitalRead (buttonPin);

    Serial.println (buttonPressed);
 
    bright = bright + dir;
    delay (100);
 
     potValue = analogRead(potPin);

  newValue = map(potValue, 0, 1023, 0, 200);

//Serial.print (potValue);

Serial.println (newValue);
 
    if (buttonPressed == 0){
      digitalWrite(ledPin, HIGH);
  dir += -1;
    }
 
    else{
      analogWrite (ledPin, newValue);
      bright = newValue;
    }

  analogWrite(ledPin, bright);

  if (bright >= 255){
    bright = 0;
  }
}

No comments:

Post a Comment