Sie sind auf Seite 1von 7

/*Program name: Blink 2 LEDs at different rates

Md Assad-Uz-Zaman, MECHENG-479, Sep. 19,2016


Lab section:
On board LED goes TURN ON for 1 sec and then TURN OFF next 1 sec. In
the meantime, the LED on
the circuit board TURN ON and OFF for every 0.25 sec.
Arduino Resources Used:
PWM pin 6, digital output, LED
PWM pin 13, digital output, On board LED
*/
void setup() {
pinMode(13, OUTPUT); // initialize digital pin 13 as an output.
pinMode(6, OUTPUT); // initialize digital pin 6 as an output.
}
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(6, HIGH); // turn the LED on (HIGH is the voltage level)

delay(250);

//wait for a .25 second

digitalWrite(6, LOW);
delay(250);

// wait for a .25 second (Total 0.50 sec)

digitalWrite(6,HIGH);
delay(250);

// wait for a .25 second (Total 0.75 sec)

digitalWrite(6, LOW);
delay(250);

// wait for a .25 second (Total 1.00 sec)

digitalWrite(13, LOW);

// turn the LED off by making the voltage LOW

digitalWrite(6, HIGH); // turn the LED on (HIGH is the voltage level)

delay(250);

//wait for a .25 second

digitalWrite(6, LOW);

delay(250);

// wait for a .25 second (Total 0.50 sec)

digitalWrite(6,HIGH);
delay(250);

// wait for a .25 second (Total 0.75 sec)

digitalWrite(6, LOW);
delay(250);

// wait for a .25 second (Total 1.00 sec)

Observed behavior:
Two LEDs operate at a different sequence at the same time. LED on the pin 13 when
turn on the LED on pin 6 complete 4 on and off cycle. Again when LED on the pin 13
when turn off the LED on pin 6 complete 4 on and off cycle.

/*Program name: Latching Switch


Md Assad-Uz-Zaman, MECHENG-479, Sep. 19,2016
Lab section:
LED goes on when button pressed and off when pressed again. In other
words a low to high
transition Causes the LED to toggle Pushbutton wired to go high when
pressed and low when
released
Arduino Resources Used:
PWM pin 6, digital output, LED
PWM pin 2, digital input, pushbutton
*/

// assigning name to the pins


const int buttonPin = 3;
// pushbutton pin
const int ledPin = 6;
// LED pin
// variables initial condition
int buttonState = LOW;
// variable for reading the pushbutton state
int previousButtonState = LOW;
// previous state of pushbutton
int ledState = HIGH;
// current state (on or off) of LED
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

// initialize the LED pin as an output


// initialize the pushbutton pin as an input

void loop()
{
buttonState = digitalRead(buttonPin);
pushbutton value

// read the state of the

if (buttonState == HIGH && previousButtonState == LOW) // Check if low to high


transition on pushbutton

{
digitalWrite(ledPin, ledState);
toggle the LED
if (ledState == LOW)
what it was for next toggle
{
ledState = HIGH;
}
else
{
ledState = LOW;
}
}
previousButtonState = buttonState;
next comparison
}

// High to low transition now


//Make ledState the opposite of

// Preserve button state for

Observed behavior:
Due to polling the program wont response if the button pressed too quickly. The
program may not executing the digitalRead() function too quickly resulting no
response from the Arduino. To avoid this add a 0.5 sec delay in our program in our
task-4.

/*
Program name: Analog input(potentiometer,digital(LED) output
Md Assad-Uz-Zaman, MECHENG-479, Sep. 26, 2016
Prints the value of the potentiometer in decimal and binary. Program
loops endlessly at 2.5 Hz. Also
the output is displaing its binary value through the 7 lED HIGH and LOW
status.For 7 LED there is larger
load on the circuit so the LEDs are connected to the Arduino board
through an amplifier chip.
Arduino resource used:
Arduino analog pin 0, analog input,Potentiometer
PWM pin 7,8,9,10,11,12,13; digital output, ULN2003 driver chip
*/
// Constants and variables section
const int PotPin = 15; // input pin for the potentiometer
int PotReading = 0;
// variable to hold potentiometer value
void setup()
{
Serial.begin(9600); // Opens the default serial channel at 9600 baud
pinMode(13,OUTPUT); // initialize pin as an output for ULN2003 pin 1B as
input(Most significant Bit)
pinMode(12,OUTPUT); // initialize pin as an output for ULN2003 pin 2B as input
pinMode(11,OUTPUT); // initialize pin as an output for ULN2003 pin 3B as input
pinMode(10,OUTPUT); // initialize pin as an output for ULN2003 pin 4B as input
pinMode(9,OUTPUT); // initialize pin as an output for ULN2003 pin 5B as input
pinMode(8,OUTPUT); // initialize pin as an output for ULN2003 pin 6B as input
pinMode(7,OUTPUT); // initialize pin as an output for ULN2003 pin 7B as
input(Lease significant Bit)
}
void loop()
{
PotReading = analogRead(PotPin); // Read the pot

int x = PotReading/8;

// convert Pot read from 10 bit to 7 bit

Serial.print("Potentiometer = "); // Beginning of message


Serial.print(x);
// potentiometer reading in decimal
Serial.print(" dec, ");
// decimal indicator in message
Serial.print(x, BIN);
// potentiometer reading in binary
Serial.println(" bin");
// binary indicator in message, puts a linefeed at end

digitalWrite(13,bitRead(x,0)); // read the HIGH or LOW condition for pin 13(Most


significant Bit)
digitalWrite(12,bitRead(x,1)); // read the HIGH or LOW condition for pin 12
digitalWrite(11,bitRead(x,2)); // read the HIGH or LOW condition for pin 11
digitalWrite(10,bitRead(x,3)); // read the HIGH or LOW condition for pin 10
digitalWrite(9,bitRead(x,4)); // read the HIGH or LOW condition for pin 9
digitalWrite(8,bitRead(x,5)); // read the HIGH or LOW condition for pin 8
digitalWrite(7,bitRead(x,6)); / / read the HIGH or LOW condition for pin 7(Lease
significant Bit)

delay(100); //waits 100 msec


}

Observed behavior:
By this program analog data from potentiometer is displayed in binary format
through 7 LEDs as a 7 bit data. If the decimal reading of potentiometer is 5 then the
sequence of the LED high and low status is 0000101.

Das könnte Ihnen auch gefallen