Friday, December 12, 2014

Arduino Code and Explaination

Here is the Arduino code we are using in our project:

int led1 = 9;
int led2 = 10;
int button = 2;

int dash = 100;

boolean buttonState = 1;

void setup(){
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);

  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);

  pinMode(button, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop(){
  boolean buttonState = digitalRead(button);

  if(buttonState == 0){
    digitalWrite(led1, HIGH);
    delay(dash);
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    delay(dash);
    digitalWrite(led2, LOW);
  }
}


The goal of this code is to have two led lights blinking alternatively back and forth when a button on the controller is pressed. The lights will be located at the tip of the gun located on the car. We felt this is a safer option opposed to shooting bbs out of an air soft gun.

The code works by reading a signal from a pin located on one of the circuit boards and using that as an input on the arduino board (the input is the button, in this case). The code then confirms if the button is being pushed or not. If it is pushed, the lights on the tip of the gun light up. If, however, the code registers that the button is not pushed, the circuit (that we have created) is not completed and thus no current flows through the lights and they do not light up.

No comments:

Post a Comment