Analog Input

Lab Exercise 1

For this Lab exercise, I set up my Arduino to read and print out the voltage readings of a switch, a potentiometer, and a photocell.   

switch_sm.gif
lightsensor_sm.gif

Code:

 

void setup() {
  // ppin mode
  pinMode(8, INPUT); //set switch's pin 8 to be input
  Serial.begin(9600); //initialize serial communications
  
  

}

void loop() {
  int SwitchState = digitalRead(8); //read the switch
  int potState = analogRead(A0); // Read the potentiometer
  // you should pause for a a millisecond b/w two analog reads. 
  //Turns out that arduino has only one to analog converter so can only do 2 at a time
  //so you need to reset it using this bit of time, this helps get a reliable read on the second analog read
 
  delay(1);  
  int photoCellState = analogRead(A1); // read the photocell

  //print them all out
  Serial.print("switch: ");
 
  Serial.print(SwitchState);
      Serial.print("\t"); //print a tab
  
  
  Serial.print("potState: ");
  Serial.print(potState);
      Serial.print("\t"); //print a tab
  
  Serial.print("photoCellState: ");
  Serial.println(photoCellState); //like enter return when typing
    


}

Lab Exercise 2

After completing lab exercise 2 - using digital or analog input and digital output, I came up with an application: The pressure sensor served as a weighing mechanism for objects I had around. The analog input was the weight of the objects and the digital output was the strings of words written by the Serial print monitor that corresponded to the weight range I assigned. I used an "if / else" statement for this.

if (lightness < 3)
{
Serial.println("Try a heavier one");
   Serial.print("\t"); 
 
}
else  {
  Serial.println("YOU DID IT");