Labs | Digital IO, Analog IO

Labs | Digital IO, Analog IO

Note: I didn't take thorough notes at the time of doing the lab so I'm recreating it with limited time. I worked with Viviene on the lab and we managed to work through all the assignments.

Digital In / Digital Out

image

image

// Code 

void setup() {
  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() {
   // read the pushbutton input:
   if (digitalRead(2) == HIGH) {
     // if the pushbutton is closed:
     digitalWrite(3, HIGH);    // turn on the green LED
     digitalWrite(4, LOW);     // turn off the red LED
   }
   else {
     // if the switch is open:
     digitalWrite(3, LOW);     // turn off the green LED
     digitalWrite(4, HIGH);    // turn on the red LED
   }
 }

Analog with an Arduino

Implemented the analog lab with the force resistors.

const int ledPrimaryPin = 2;       // pin that the LED is attached to
const int ledSecondayPin = 3;      // pin that the LED is attached to
int analogValueForceResistorA = 0;  // value read from the pot
int analogValueForceResistorB = 0;  // value read from the pot
int forceA = 0;                     // force value
int forceB = 0;                // PWM pin that the LED is on.
int minn = 100;
int maxx = -100;

void setup() {
    // Initialize serial communications at 9600 bps:
    Serial.begin(9600);
    
    // Declare the output pins
    pinMode(ledPrimaryPin, OUTPUT);
    pinMode(ledSecondayPin, OUTPUT);
}

void loop() {
    // Declare the input pin
    analogValueForceResistorA = analogRead(A0);
    analogValueForceResistorB = analogRead(A1);

    // map 
    // 1,2,3,4,5,6,7,8,9,10 => 0 => 255
    // 25.5, 51, 76.5 ... 255

    // Calculate incoming
    forceA = max(0, map(analogValueForceResistorA, 50, 1012, 0, 255));
    forceB = max(0, map(analogValueForceResistorB, 50, 1023, 0, 255));
    
    analogWrite(ledPrimaryPin, forceA);
    analogWrite(ledSecondayPin, forceB);

    // 11111111 255

    // Min - Max
     minn = min(analogValueForceResistorA, minn); 
     maxx = max(analogValueForceResistorA, maxx);
     Serial.println(minn);
     Serial.println(maxx);
}

I followed through on the core lab and then adapted the circuit and sensor readings to work with the Myoware Muscle Kit Sensor. I didn't have to change much. The only thing that was different was that the mapping was form 0 to 10 and I turned the light on after a certain threshold (5)

image