Switch (переключатель) на входе датчика

Алгоритмы
Switch (переключатель) на входе датчика

Скетч сначала считывает информацию с фоторезистора. Затем он использует функцию map(), чтобы присвоить его выходам одно из следующих значений: 0, 1, 2 или 3. Наконец, он использует switch(), чтобы вывести на компьютер одно из четырех сообщений, в зависимости от того, какой из четырех значений возвращен.
Фоторезистор соединён с аналоговым пином 0 с помощью кругооборота сепаратора напряжения. 10 К Ом-резистор составляет другую сторону сепаратора напряжения, идущего от пина 0 к земле. Функция analogRead() возвращает диапазон приблизительно от 0 до 600 этого цикла.



Схема



Код

 /*
   Switch statement
  
  Demonstrates the use of a switch statement.  The switch
  statement allows you to choose from among a set of discrete values
  of a variable.  It's like a series of if statements.
  
  To see this sketch in action, but the board and sensor in a well-lit
  room, open the serial monitor, and and move your hand gradually
  down over the sensor.
  
  The circuit:
  * photoresistor from analog in 0 to +5V
  * 10K resistor from analog in 0 to ground
  
  created 1 Jul 2009
  by Tom Igoe 
  
  http://www.arduino.cc/en/Tutorial/SwitchCase
  */

 // these constants won't change:
 const int sensorMin = 0;      // sensor minimum, discovered through experiment
 const int sensorMax = 600;    // sensor maximum, discovered through experiment

 void setup() {
   // initialize serial communication:
   Serial.begin(9600);  
 }

 void loop() {
   // read the sensor:
   int sensorReading = analogRead(0);
   // map the sensor range to a range of four options:
   int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

   // do something different depending on the 
   // range value:
   switch (range) {
   case 0:    // your hand is on the sensor
     Serial.println("dark");
     break;
   case 1:    // your hand is close to the sensor
     Serial.println("dim");
     break;
   case 2:    // your hand is a few inches from the sensor
     Serial.println("medium");
     break;
   case 3:    // your hand is nowhere near the sensor
     Serial.println("bright");
     break;
   } 

 } 


Оригинал

Switch (переключатель) на последовательном входе
technik 21.12.2009 в 23.02 (обновлен 21.12.2009 в 23.02) комментарии 0 0

комментарии(0)

Комментировать

Для добавления комментария авторизируйтесь.
Последние комментарии:
© 2012 pobot.ru