Monday, April 29, 2013

Week 7: Coding Idea

At this stage I am working with Coding and Arduino.


Below are the code that I had.

Basiclly what the code does is commanding the led light red and yellow to turn on and off by 2 push buttons. I decided to start of with this set of code as it can be applied to the code for the model of project. The button act as an INPUT (gustier recognition) and the LEDlight act as an OUTPUT (servo). The servo also move into left direction when button 1 is pressed and another when button 2 is pressed.

The code is only used to guide my way on track and get the idea onto the project.




#include <Servo.h>

Servo myservo;

const int buttonUpPin = 2;  
const int buttonDownPin = 3;
const int ledPin =  13;    
const int ledPinYellow =12;

int pos = 0;

int buttonUpState = 0;      
int buttonDownState =0;

void setup() {

   myservo.attach (9);
 
  pinMode(ledPin, OUTPUT);
  pinMode(ledPinYellow, OUTPUT);

  pinMode(buttonUpPin, INPUT);  
   pinMode(buttonDownPin, INPUT);

}

void loop(){

   buttonUpState = digitalRead(buttonUpPin);
   buttonDownState = digitalRead(buttonDownPin);

  if (buttonUpState == LOW) {  
     
    digitalWrite(ledPin, HIGH);
 
    for(pos = 0; pos < 180; pos += 1)
  {                                
    myservo.write(pos);            
 
  }
  }
  else {
 
    digitalWrite(ledPin, LOW);
  }

  if (buttonDownState == LOW) {
    digitalWrite(ledPinYellow, HIGH);
 
      for(pos = 180; pos>=1; pos-=1)  
  {                              
    myservo.write(pos);            
     
  }
  }
  else {
    digitalWrite(ledPinYellow, LOW);
  }
}


No comments:

Post a Comment