Lab
Today we started with our first Arduino lab. We first wanted to make one LED on a breadboard blink, then get 4 LEDs to blink. I was able to do this successfully using the code below.
void setup() {
// put your setup code here, to run once:
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(3, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(11, HIGH);
delay(500);
digitalWrite(11,LOW);
digitalWrite(10,HIGH);
delay(500);
digitalWrite(10,LOW);
digitalWrite(9,HIGH);
delay(500);
digitalWrite(9,LOW);
delay(100);
digitalWrite(11, HIGH);
digitalWrite(3,HIGH);
digitalWrite(10,HIGH);
digitalWrite(9,HIGH);
delay(100);
digitalWrite(11, LOW);
digitalWrite(3,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
delay(100);
digitalWrite(11, HIGH);
digitalWrite(3,HIGH);
digitalWrite(10,HIGH);
digitalWrite(9,HIGH);
delay(100);
digitalWrite(11, LOW);
digitalWrite(3,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
delay(100);
digitalWrite(11, HIGH);
digitalWrite(3,HIGH);
digitalWrite(10,HIGH);
digitalWrite(9,HIGH);
delay(100);
digitalWrite(11, LOW);
digitalWrite(3,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
delay(100);
digitalWrite(11, HIGH);
digitalWrite(3,HIGH);
digitalWrite(10,HIGH);
digitalWrite(9,HIGH);
delay(1000);
digitalWrite(11, LOW);
digitalWrite(3,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
delay(500);
}
This code would cause the top three lights to light up one after the other from top to bottom, then all four lights would blink on and off 3 times, then turn on again for a second, and then the cycle would repeat.
#define PI 3.14159265
This would allow someone to use PI to perform math operations, rather than typing in the number every time.
I learned that the order of operations in C is the same as in mathematics.
Next, we learned about increment and decrement operators. They were:
#include <stdio.h>
#include <math.h>
int main(void){
float femur_length,humerus_length;
float femur_height_female,humerus_height_female,avg_height_female;
float femur_height_male,humerus_height_male,avg_height_male;
scanf("%f %f",&femur_length,&humerus_length);
femur_height_female=femur_length*1.94+28.7;
humerus_height_female=(humerus_length*2.8)+28.2;
avg_height_female=(femur_height_female+humerus_height_female)/2;
femur_height_male=(femur_length*1.88)+32;
humerus_height_male=(humerus_length*2.9)+27.9;
avg_height_male=(femur_height_male+humerus_height_male)/2;
printf("Female Femur Height %5.2f \n Female Humerus Height %5.2f \n Female Average Height %5.2f \n Male Femur Height %5.2f \n Male Humerus Height %5.2f \n Male Average Height %5.2f \n",femur_height_female,humerus_height_female,avg_height_female,femur_height_male,humerus_height_male,avg_height_male);
return 0;
}
Today we started with our first Arduino lab. We first wanted to make one LED on a breadboard blink, then get 4 LEDs to blink. I was able to do this successfully using the code below.
void setup() {
// put your setup code here, to run once:
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(3, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(11, HIGH);
delay(500);
digitalWrite(11,LOW);
digitalWrite(10,HIGH);
delay(500);
digitalWrite(10,LOW);
digitalWrite(9,HIGH);
delay(500);
digitalWrite(9,LOW);
delay(100);
digitalWrite(11, HIGH);
digitalWrite(3,HIGH);
digitalWrite(10,HIGH);
digitalWrite(9,HIGH);
delay(100);
digitalWrite(11, LOW);
digitalWrite(3,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
delay(100);
digitalWrite(11, HIGH);
digitalWrite(3,HIGH);
digitalWrite(10,HIGH);
digitalWrite(9,HIGH);
delay(100);
digitalWrite(11, LOW);
digitalWrite(3,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
delay(100);
digitalWrite(11, HIGH);
digitalWrite(3,HIGH);
digitalWrite(10,HIGH);
digitalWrite(9,HIGH);
delay(100);
digitalWrite(11, LOW);
digitalWrite(3,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
delay(100);
digitalWrite(11, HIGH);
digitalWrite(3,HIGH);
digitalWrite(10,HIGH);
digitalWrite(9,HIGH);
delay(1000);
digitalWrite(11, LOW);
digitalWrite(3,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
delay(500);
| 4 LEDs blinking |
This code would cause the top three lights to light up one after the other from top to bottom, then all four lights would blink on and off 3 times, then turn on again for a second, and then the cycle would repeat.
Lecture
After we finished the lab, we went over efficient ways to do math in C. We reviewed symbolic constants, which can be defined like so:
#define PI 3.14159265
This would allow someone to use PI to perform math operations, rather than typing in the number every time.
I learned that the order of operations in C is the same as in mathematics.
Next, we learned about increment and decrement operators. They were:
- ++ to increment by 1
- -- to decrement by 1
- +=k to increment by k
- -=k to decrement by k
#include <stdio.h>
#include <math.h>
int main(void){
float femur_length,humerus_length;
float femur_height_female,humerus_height_female,avg_height_female;
float femur_height_male,humerus_height_male,avg_height_male;
scanf("%f %f",&femur_length,&humerus_length);
femur_height_female=femur_length*1.94+28.7;
humerus_height_female=(humerus_length*2.8)+28.2;
avg_height_female=(femur_height_female+humerus_height_female)/2;
femur_height_male=(femur_length*1.88)+32;
humerus_height_male=(humerus_length*2.9)+27.9;
avg_height_male=(femur_height_male+humerus_height_male)/2;
printf("Female Femur Height %5.2f \n Female Humerus Height %5.2f \n Female Average Height %5.2f \n Male Femur Height %5.2f \n Male Humerus Height %5.2f \n Male Average Height %5.2f \n",femur_height_female,humerus_height_female,avg_height_female,femur_height_male,humerus_height_male,avg_height_male);
return 0;
}
Comments
Post a Comment