[p-com wk 2] Programming ! Digital and analog input !

So, here’s a striking realization from last week. I completed my lab very early in the week, felt good about myself, and called it a day. A few days later, a cohort on the floor seek help. I was surprised to find out that, despite being able to diagnose the problem, I struggled so much trying to demonstrate wiring the circuit correctly. It turned out that my half knowledge of theories needed a lot of trials and errors to translate to physical understanding.

To make sure I actually know how to put physics into making, I decided to complete all my circuits this week strictly according to their schematic diagrams, which means no referencing to breadboard photos. 

I also like to make them extremely neat

Digital Input and Output

The first setup consists of two LEDs in parallel, and a push button that will function as a digital input. I plugged it in; a problem evolved immediately: Arduino could’t find my board. The port didn’t show up in the software.

Tom Igoe dropped by to help. We tried to eliminate one element and then another: made sure my Macbook was alright, the cable was working, as well as the hub that connects it to my laptop, and then that my Arduino nano board was fine. We restarted the software several times, and eventually it worked. Even though the issue is still unclear, an important lesson of trouble-shooting was learned, and later this “one at a time” method enabled me to lend a helping hand to other students on the floor. 

Later on, I realized that with each upload I had to quit Arduino and restart in order for it to recognize my board again. Some other cohorts were having the same issue, and its cause / long term solution remain unknown. Despite all those roadblocks, my code was finally up and running.

void setup() {
  pinMode(2, INPUT);  
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop() {
   if (digitalRead(2) == HIGH) {
     digitalWrite(3, HIGH);
     digitalWrite(4, LOW);
   }
   else {
     // if the switch is open:
     digitalWrite(3, LOW); 
     digitalWrite(4, HIGH); 
   }
}

Since the LED setup succeeded, I swapped one out for an 8ohm speaker, and once again it did not work. After making sure my circuit was correct, I ran multimeter tests and found out that Voltage through the speaker was only 0.058V, which seemed very low, comparing to 1.9V in the LED. I changed the 220 ohm resistor into a 100 ohm one, and later 50 ohm, but none helped. Later while inspecting the code, David (who knows a lot about sound) instructed me to add a delay function, and it worked wonders.

I would still like to know how much voltage is needed / necessary / safe for the speaker, especially since I found it on the floor so no datasheet was available at hand.

Here’s the code that worked:

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT); 
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}
void loop() {
   if (digitalRead(2) == HIGH) {
   // if the pushbutton is closed:
   // turn off speaker
       noTone(3); 
   }
   else {
   // if the switch is open:
   // turn on speaker
     tone(3, 48zx 0); 
     delay(120);
   }
}

Analog Input

Since using a voltage regulator last week, I’ve been confused about three legged components, because according my previous, very limited middle school soldering club experience, everything needed to have two pins and two pins only, “positive” and “negative”. After actually working with a microcontroller, unlike the simple circuits I made in middle school, and reading about what “ground” is, I was finally able to understand the three-pin situation. Specifically, on a potentiometer – power, output, ground.

With the help of the potentiometer, partial code provided by lab instruction, and remembering to add “delay”, I was able to make the speaker change tones!

const int pin = 9;  //speaker pin 
  int analogValue = 0;
  int frequency = 0;
void setup() {
   Serial.begin(9600);
   pinMode(pin, OUTPUT);
}

void loop() {
    analogValue = analogRead(A0); 
    frequency = (analogValue /4) * 10; 
    // I don't understand. What's a byte? why /4?
    tone(pin, frequency);
    // make a changing tone on the speaker
    delay (150); //!!essential!!
    Serial.println(frequency);
}

There are two peculiar questions I have not yet found answers to:

  1. the line that says frequency = (analogValue /4) * 10; , I did not understand what it meant. Why divided by 4 ?
  2. As seen in the video, once my potentiometer left the board, the speaker kept making sound in random pitches. In theory, when analogValue returns to 0, the pitch should be zero as well, no?

Moving on to pressure sensors, I, once again, spent an unbelievable amount of time debugging. I wired up the circuit board according to the schematic diagram: two pressure sensors in parallel (?) as Input, and two LEDs as Output. I was happy to learn about the map function, however, the initial mapping (from 400-900) didn’t work for me. When I wrote map(topSensorValue, 400, 900, 0, 255); , the two LED lit up immediately, and responded not very well to pressure, only flickering occasionally.

I used Serial Monitor in the Arduino software to find out that my sensor’s range was between 0 to 900, so I, knowing fully that this could be very wrong, changed the value map to 0 to 900… and surprisingly, it worked.

Here is my code, and video evidence of that accidental miracle:

const int yellowLED = 9;     
const int greenLED = 10;   
int topSensorValue = 0;  
int bottomSensorValue = 0;  

void setup() {
  Serial.begin (9600);
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
}

void loop() {
  topSensorValue = analogRead(A0); 
  int brightness = map(topSensorValue, 0, 900, 0, 255); 
  //It only worked when I mapped from 0-900 instead of 400-900
  analogWrite(greenLED, brightness);  
  Serial.println(brightness);  

  bottomSensorValue = analogRead(A1); 
  brightness = map(bottomSensorValue, 0, 900, 0, 255);
  analogWrite(yellowLED, brightness);
  Serial.println(brightness);

I would like to test speaker with it too, but honestly couldn’t understand how to change the code. Therefore, it will not be done.

On an unrelated note:

When I was soldering wires onto the speaker, I realized that last time I soldered was in middle school… almost 10 years ago. I remembered how powerful and energized I felt when I first held a soldering iron at the age of 14. My middle school was oppressive with an authoritarian school board and a bully-dominating social environment, but there I discovered a “semi-conductor” club that did easy circuits and soldering projects. The entire club had 6 members, all boys, nerdy boys, and the girls laughed at me for expressing interests because it was thought to be a “lame” club, advised by one of the least popular teachers. I’ve also been historically bad at math and was convinced that I won’t be a scientist or engineer, and I didn’t think I would like to be. But there was a part of me, I think, that always wanted to prove them wrong, that found tremendous joy in building, making, calculating, and problem solving. Here on the ITP floor, 11:03 pm, I’m starting to reconnect with that little person who would stay in school till it was pitch dark, building easy circuits and shutting the entire world behind them. I’m so glad that little spark is still there, and I can’t wait for the p-com journey waiting ahead.

See you next week!

Leave a Reply

Your email address will not be published. Required fields are marked *