Friday, November 1, 2024

Playing With Digital and Analog Pins

The first step was to wire up a potentiometer, which worked great once I got the LED pointed the right direction!

/*
Use Potentiometer to control blinking speed
*/

// Use global variables for pin numbers
int sensorPin = 0;    // Connect potentiometer to analog pin 0
int ledPin = 13;      // Connect LED to digital pin 13


void setup() {

  // Same as "pinMode(13, OUTPUT);"  
  pinMode(ledPin, OUTPUT);

  // Don't need to configure potentiometer bc it's using an
  // analog in pin, so it's always expecting an input
 
  // Do this so we get outputs for troubleshooting if necessary
  Serial.begin(9600);
}


void loop() {
 
  // Declare integer sensorValue to read in each time
  int sensorValue;

  /* Use analogRead() to get a value from potentiometer 0 to 1023.  
  Since we've plugged into the 5V power source, that'll get scaled
  from 0 to 5 V .*/

  sensorValue = analogRead(sensorPin);    

  // Print the sensorValue to the serial monitor
  Serial.println(sensorValue);

  /* Now we'll blink the LED like in the first example, but we'll
  use the sensorValue variable to change the blink speed
 (the smaller the number, the faster it will blink). */

  digitalWrite(ledPin, HIGH);     // Turn the LED on
  delay(sensorValue);             // Pause for sensorValue milliseconds
 
  digitalWrite(ledPin, LOW);      // Turn the LED off
  delay(sensorValue);             // Pause for sensorValue milliseconds
 
}



With that going through, the next step was to do some exploration.

See what happens if you use two digital pins rather than on digital and one analog pin.

Moving to a digital pin, I made some adjustments and assumptions.  I used the #12 digital pin, and adjusted the code as follows:

int ledPin = 13;      // Connect LED to digital pin 13

and 

// Let's assume since there was an analogRead() that there's also a digitalRead()
sensorValue = digitalRead(sensorPin);    

Looking back into the void setup() function, I thought maybe I needed to add this line, but it worked fine without it.

  // Assuming if there's an OUTPUT there's also an INPUT
  pinMode(sensorPin, INPUT);



Since I had coded it to view the value of sensorPin in the terminal, I could see that in fact, it was either reading a zero (never on) or a one (always on).  This makes sense, since the pin is digital, it can only read a zero or a 1.

See what happens if you use two analog pins rather than one digital and one analog pin; and what happens if you replace analogWrite with digitalWrite and vice versa?

This went over quite easily actually, just had to change three lines of code:

// Use global variables for pin numbers
int sensorPin = A0;    // Connect potentiometer to analog pin A0
int ledPin = A1;       // Connect LED to digital pin A1

and

  // Let's assume since there was an analogRead() that there's also a digitalRead()
  sensorValue = analogRead(sensorPin);  

Interestingly enough, I didn't change these lines to analogWrite and instead left them as digitalWrite.  Attempting to put them in as analogWrite made the board not function, which makes sense since we want the LED to be either ON or OFF, a digital value.

  digitalWrite(ledPin, HIGH);     // Turn the LED on
  digitalWrite(ledPin, LOW);      // Turn the LED off

Instead, I needed to adjust the inputs to that function:

  analogWrite(ledPin, 255);       // Turn the LED on
  analogWrite(ledPin, 0);      // Turn the LED off

For extra funzies, I decided to experiment with the website https://www.tinkercad.com/.  With some blips and spurts and backwards LEDs (always, always, the backwards LEDs!!!),   I was able to simulate the entire setup and copy/paste the code from the Arduino IDE.


If you want to, you can play along here!  Tinkercad - Potentiometer Options

What's even better, is that it made its own circuit diagram, which is much better than the one I was getting out of MATLAB last week!



No comments:

Post a Comment

Nooooo, don't make me reflect on my learning!

 ... I just want to stick wires in breadboards and make froggies dance!!! Okay, but seriously, that stupid dancing frog was clearly my most ...