Skip to content

13. Input devices

Assignment

group assignment : probe an input device’s analog levels and digital signals

individual assignment : measure someting : add a sensor to a microcontroller board that you have designed an read it

Input devices

Button

Capacitive sensors

CapacitiveSensor library here.

More informations on serial print here.

#include <CapacitiveSensor.h>


CapacitiveSensor   cs_2_3 = CapacitiveSensor(2,3);        // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil
CapacitiveSensor   cs_2_4 = CapacitiveSensor(2,4);
int lastbutton = 0;
const int buttonmi = 2;
const int buttonla = 5;

void setup()                    
{
   pinMode(10, OUTPUT);
   cs_2_3.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate
   Serial.begin(9600);

}

void loop()                    
{
    long start = millis();
    long total1 =  cs_2_3.capacitiveSensor(30);
    long total2 =  cs_2_4.capacitiveSensor(30);

if (total1 >= 100 && lastbutton != buttonmi) {
  tone(10, 311);
  lastbutton = buttonmi;
}
else if(total2 >= 100 && lastbutton != buttonla) {
  tone(10, 440);
  lastbutton = buttonla;
}
else if(total1 < 100
    && total2 < 100){
      noTone(10);
      lastbutton = 0;
    }
    Serial.print(millis() - start);        // check on performance in milliseconds
    Serial.print("\t");                    // tab character for debug window spacing
    Serial.print(total1);
    Serial.print("\t");
    Serial.println(total2);

    delay(10);                             // arbitrary delay to limit data to serial port
}

Last update: May 13, 2022
Back to top