đź‘ľ

Arduino: Week 2

Arduino: Week 2

Labs and resources used

Lab:  Digital Input and Output with an Arduino – ITP Physical ComputingLab: Digital Input and Output with an Arduino – ITP Physical Computing

ITP_NYU Digital Output - Intro to Pcomp Lesson 1ITP_NYU Digital Output - Intro to Pcomp Lesson 1

Microcontroller Pin Functions – ITP Physical ComputingMicrocontroller Pin Functions – ITP Physical Computing

allaboutcircuit Resistor Color Code Calculator and Chart—4 Band, 5 Band, or 6 Band Resistors - Engineering Calculators & Toolsallaboutcircuit Resistor Color Code Calculator and Chart—4 Band, 5 Band, or 6 Band Resistors - Engineering Calculators & Tools

docs.arduino.ccdocs.arduino.cc

Lab: Analog In with an Arduino – ITP Physical ComputingLab: Analog In with an Arduino – ITP Physical Computing

Related Video: Wiring an FSR (force sensitive resistor)

Related Video: Wiring a photocell to measure light

First test

image

1 digital input and 2 digital outputs

image
void setup() {
  // put your setup code here, to run once:
  pinMode(2, INPUT);    // set the pushbutton pin to be an input
  pinMode(3, OUTPUT);   // set the yellow LED pin to be an output
  pinMode(4, OUTPUT);   // set the red LED pin to be an output
}

void loop() { 
  // put your main code here, to run repeatedly:
  
  // read the pushbutton input:
   if (digitalRead(2) == HIGH) {
     // if the pushbutton is closed:
     digitalWrite(3, HIGH);    // turn on the yellow LED
     digitalWrite(4, LOW);     // turn off the red LED
   }
   else {
     // if the switch is open:
     digitalWrite(3, LOW);     // turn off the yellow LED
     digitalWrite(4, HIGH);    // turn on the red LED
   }
}

Using potentiometer, speaker and arduino

image
const int pinNumber = 9;       // pin that the LED is attached to
int analogValue = 0;        // value read from the pot
int brightness = 0;         // PWM pin that the LED is on.
int frequency = 0; 

void setup() {
  // put your setup code here, to run once:
  
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  // declare the led pin as an output:
  pinMode(pinNumber, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  analogValue = analogRead(A0);      // read the pot value
  frequency = (analogValue /4) * 10; // divide by 4 to fit in a byte, multiply by 10 for a good tonal range
  tone(pinNumber, frequency);        // make a changing tone on the speaker
  Serial.println(brightness);        // print the brightness value back to the serial monitor
}

Finding sensor’s range - with bug

image
const int redLED = 10;     // pin that the red LED is on
const int greenLED = 11;   // pin that the green LED is on
int rightSensorValue = 0;  // value read from the right analog sensor
int leftSensorValue = 0;   // value read from the left analog sensor

void setup() {
  // put your setup code here, to run once:
  
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  // declare the led pins as outputs:
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  rightSensorValue = analogRead(A0); // read the pot value
  // map the sensor value from the input range (400 - 900, for example)
  // to the output range (0-255). Change the values 400 and 900 below
  // to match the range your analog input gives:
  int brightness = map(rightSensorValue, 400, 900, 0, 255);
  analogWrite(redLED, brightness);  // set the LED brightness with the result
  Serial.println(rightSensorValue);   // print the sensor value back to the serial monitor

  // now do the same for the other sensor and LED:
  leftSensorValue = analogRead(A1); // read the pot value
  // map the sensor value to the brightness again. No need to
  // declare the variable again, since you did so above:
  brightness = map(leftSensorValue, 400, 900, 0, 255);
  analogWrite(greenLED, brightness);  // set the LED brightness with the result
  Serial.println(leftSensorValue);   // print the sensor value back to the serial monitor
}
image

Missing labs from this week

Lab: Sensor Change Detection – ITP Physical ComputingLab: Sensor Change Detection – ITP Physical Computing

Lab: Intro to Asynchronous Serial Communications – ITP Physical ComputingLab: Intro to Asynchronous Serial Communications – ITP Physical Computing