Lab 2: digital input and output + Lab 3: analog input

Well, isn’t it about failing after all?

I followed both labs exactly as they are in the syllabus (Lab2, Lab3), and it all went just fine. Here are two gifs of the results:

 

After that, I started working in my last week’s project again. I had many different switches and sensors in that project, but all working only with electricity. It was all connected in series, so that you could only turn on an LED when the ones before them were on. My intent was to connect each switch to an input, each LED to an output, and make a code so that the LEDs would turn on depending on whether all the LEDs before it were on, such as is was in the first circuit.

pcomp lab 1.gif

First, I connected all the switches only to the controller, and uploaded the code. The LEDs obviously acted very weird, I don’t really know to explain what kind of signals they were getting.

IMG_1779.JPG

I then connected the potentiometer to power and ground, moved the switches to digital inputs, and added what I believed to be pulldown resistors to them. The switches wouldn’t respond, and here’s what happened when I moved the potentiometer:

I was sure there was something wrong with the pushbutton, so I tested it , changed positions and switched the components, but didn’t get any better results. I didn’t know if I should ground the photocell and thermistor in the same way or not, so I tried doing it, and the LEDs just wouldn’t turn on.

IMG_1781.JPG

After that, I didn’t know what else to try, so I gave up at that point. I made many modifications to the code during this whole process, and I haven’t found a decent way to put it in here, so this is the last code I was using:

// the setup function runs once when you press reset or power the board

int pt = 0;
int pb = 0;
int ts = 0;
int ph = 0;
int th = 0;
const int L1 = 10;
const int L2 = 8;
const int L3 = 6;
const int L4 = 4;
const int L5 = 2;

void setup() {
Serial.begin(9600);

// pushbutton
pinMode(13, INPUT);

//toggle switch
pinMode(12, INPUT);

// led1
pinMode(L1, OUTPUT);

// led 2
pinMode(L2, OUTPUT);

// led 3
pinMode(L3, OUTPUT);

// led 4
pinMode(L4, OUTPUT);

// led 5
pinMode(L5, OUTPUT);

}

// the loop function runs over and over again forever
void loop() {
// potentiometer
pt = analogRead(A0);

// pushbutton
pb = digitalRead(13);

// toggle switch
ts = digitalRead(12);

// photocell
ph = analogRead(A4);

// thermister
th = analogRead(A5);

analogWrite (L1, pt / 4);
if (digitalRead(12) == HIGH) {

digitalWrite(L2, HIGH);
}

if (digitalRead(13) == HIGH) {
digitalWrite(L3, HIGH);
}

analogWrite(L4, ph / 4);
analogWrite(L5, th / 4);

}

 

[EDIT: I switched the pulldown resistors for 10k ones, and now everything works perfectly.]

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair /  Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair /  Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair /  Alterar )

Conectando a %s