IFD:Interaktive Elektronik 2012/code: Difference between revisions

From Medien Wiki
mNo edit summary
mNo edit summary
 
Line 1: Line 1:
=== resistive Sensor ===
== resistive Sensor ==


[[File:Resistive_Sensor_Steckplatine.png|thumb]]
[[File:Potentiometer_A0_Serial_Steckplatine.png|thumb]]
Recipe for resistive sensor:
* Pick a resistive sensor
* Measure its lowest and highest resistance using a multimeter.
* Write the name of the sensor and the maximum and minimum resistance on the black board.
* pick a resistor which has the same value as the sensor on its highest value
* Use the below code and create the circuit for a resistive sensor
* open the serial monitor and have a look at the values.
* try larger and smaller resistors and see the difference.
* use an other sensor (one that is already listed on the black board)
Recipe for Potentiometer:
* connect the Potentiometer as displayed on the right.
* use the same program as before for the analog sensor
* see what values you get - compare with values from the resistive sensor.


[[File:Resistive_Sensor_Steckplatine.png]]


<source lang="c">
<source lang="c">
Line 17: Line 35:
}
}
</source>
</source>
<br clear="all">


=== switch or Button ===
== switch or Button ==


[[File:Button_1k_Pullup_Steckplatine.png]]
[[File:Button_1k_Pullup_Steckplatine.png|thumb]]


<source lang="c">
<source lang="c">
Line 43: Line 62:
}
}
</source>
</source>
<br clear="all">

Latest revision as of 14:28, 9 May 2012

resistive Sensor

Resistive Sensor Steckplatine.png
Potentiometer A0 Serial Steckplatine.png

Recipe for resistive sensor:

  • Pick a resistive sensor
  • Measure its lowest and highest resistance using a multimeter.
  • Write the name of the sensor and the maximum and minimum resistance on the black board.
  • pick a resistor which has the same value as the sensor on its highest value
  • Use the below code and create the circuit for a resistive sensor
  • open the serial monitor and have a look at the values.
  • try larger and smaller resistors and see the difference.
  • use an other sensor (one that is already listed on the black board)

Recipe for Potentiometer:

  • connect the Potentiometer as displayed on the right.
  • use the same program as before for the analog sensor
  • see what values you get - compare with values from the resistive sensor.


int mySensorValue;

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

void loop () {
  mySensorValue = analogRead(A0);
  Serial.print("The Sensor Value is: ");
  Serial.println(mySensorValue);
}


switch or Button

Button 1k Pullup Steckplatine.png
int mySensorValue;

void setup () {
 Serial.begin(9600);
 pinMode(13,OUTPUT);
 pinMode(4,INPUT);
}

void loop () {
  mySensorValue = digitalRead(4);
  if (mySensorValue == LOW) {
    Serial.println("The Button is pressed. ");
    digitalWrite(13,HIGH);
  }
  else {
    Serial.println("The Button is not pressed");
    digitalWrite(13,LOW);
  } 
}