Analog Lab [ sep 21 2010 ]

For this week’s lab, we experimented with the analog outputs and the PWMs (Pulse Width Modulation) outputs on the Arduino board.PWM pins are digital pins, but because it causes the LED to blink on and off so quickly, you only see a gradual change of brightness.

In the setup above, if a person presses hard on the pressure variable resistor, the LED would dim its light, and returning to the original brightness once released. The code below programs both LEDs to react to its respective resistors.

int potPin1=0; // Assigns var potPin1 = 0
int potPin2=1; // Assigns var potPin2 = 1

int sensorValue1= 0; // sets up variable sensorValue1
int sensorValue2=0;// sets up variable sensorValue2
int led = 9; // var led is set to pin 9
int ledd = 10;// var ledd is set to pin 10

void setup(){
  Serial.begin(9600);// initialize serial communications at 9600 bps:
  pinMode(led,OUTPUT);//led from pin 9 is set as an output
  pinMode(ledd,OUTPUT);//ledd from pin 10 is set as an output
}

void loop(){
  sensorValue1=analogRead(potPin1); // Read analog value of analog pins 0 (potPin1 = 0)
  sensorValue2=analogRead(potPin2); // and pin 1 (potPin2=1)
  int brightness1= map(sensorValue1, 400,900,0,255);// var brightness pas the value of sensorValue1 and sensorValue 2
  int brightness2= map(sensorValue2, 400,900,0,255);// to digital range between 0 and 255

  analogWrite(led,brightness1);// set the led brightness1 with the result
  Serial.println(sensorValue1);// print the pot value back to the debugger pane
  delay(10);//wait 10 secs before starting loop again

  analogWrite(ledd,brightness2);// set  the ledd brightness 2 with the result in digital range
  Serial.println(sensorValue2);// print the pot value back to debugger pane
  delay(10); wait 10 secs before starting loop again
}

Initially the code was not working properly, and both LEDs were reacting to only one pressure variable resistor. I had mistakenly  set potPin1 and potPin2 both to 0 (zero), this resulted later in subsequent lines a analog read from the same analog pin 0. To fix this, I set the latter potPin 2 to 1. Thus one line of code performed an analogRead of potPin1 or analog input pin 0, and the other on potPin2 or analog input pin 1.

Posted: September 22nd, 2010
Categories: Arduino, Physical Computing
Tags:
Comments: No Comments.