top of page

Electronics

Class 2- Arduino

​

//Define variables

//LED pins connected to:

const int Red_Led_Pin=3;

const int Yellow_Led_Pin=4;

const int Green_Led_Pin=5;

 

const int button_pin=7;

const int poten_pin= A0;

 

//define variable to hold value from potentiometer

int poten_val;

// define a boolean flag

bool button_state= false;

 

//function defenitions

void setup() {

  //Define the led pins as output so that they can light:

  pinMode(Red_Led_Pin, OUTPUT);

  pinMode(Yellow_Led_Pin, OUTPUT);

  pinMode(Green_Led_Pin, OUTPUT);

  //define the button pin as pullup so it can act as a button:

  pinMode(button_pin,INPUT_PULLUP);

  //OPEN SERIAL COMMUNICATION WITH THE COMPUTER, SO WE CAN SEE THE POTENTIOMETERS

  Serial.begin(9600);

 

}

 

//Runing codes

void loop() {

  // put your main code here, to run repeatedly:

  poten_val= analogRead(poten_pin);

  Serial.println(poten_val);

  delay(500);

  Serial.println(digitalRead(button_pin));

  delay(100);

  if(digitalRead(button_pin)==0){

    button_state = !button_state;

  }

  if(button_state== true)

  {

 

     poten_val=analogRead(poten_val);

 

    if (poten_val>=682)

    {

      digitalWrite(Red_Led_Pin,HIGH);

      digitalWrite(Yellow_Led_Pin,LOW);

      digitalWrite(Green_Led_Pin,LOW);

     

    }

    else if(poten_val>=341)

    {

      digitalWrite(Red_Led_Pin,HIGH);

      digitalWrite(Yellow_Led_Pin,HIGH);

      digitalWrite(Green_Led_Pin,LOW);

    }

    else{

      digitalWrite(Red_Led_Pin,HIGH);

      digitalWrite(Yellow_Led_Pin,HIGH);

      digitalWrite(Green_Led_Pin,HIGH);

    }

    delay(100);

  }

  else{

      digitalWrite(Red_Led_Pin,LOW);

      digitalWrite(Yellow_Led_Pin,LOW);

      digitalWrite(Green_Led_Pin,LOW);

  }

}

bottom of page