コード例 #1
0
void loop() {
  DHT22_ERROR_t errorCode;
  lcd.setCursor(0, 0);
  delay(2000);
  lcd.print('Requesting Data');
  errorCode = rht.readData();
  lcd.clear();
  switch(errorCode) {
    case DHT_ERROR_NONE:
      lcd.print('T:  ' << rht.getTemperatureC() << 'C');
      lcd.setCursor(0, 1);
      lcd.print('RH: ' << rht.getHumidity() << '%');
      break;
    case DHT_ERROR_CHECKSUM:
      lcd.print('T:  ' << rht.getTemperatureC() << 'C *');
      lcd.setCursor(0, 1);
      lcd.print('RH: ' << rht.getHumidity() << '% *');
      break;
    case DHT_BUS_HUNG:
      printError('BUS Hung');
      break;
    case DHT_ERROR_NOT_PRESENT:
      printError('Not Present');
      break;
    case DHT_ERROR_ACK_TOO_LONG:
      printError('ACK timeout');
      break;
    case DHT_ERROR_SYNC_TIMEOUT:
      printError('Sync timeout');
      break;
    case DHT_ERROR_DATA_TIMEOUT:
      printError('Data timeout');
      break;
    case DHT_ERROR_TOOQUICK:
      printError('Poll too quickly');
      break;
  }
}
コード例 #2
0
ファイル: Sensors.cpp プロジェクト: iRoboticist/arduino
int SENSORS::getSensorValue(byte port, int deviceID)
{
	int aInPin;
	int dInPin;
	int sensorValue;
	static int tempValue;
	
	if(port == 1){
		aInPin = ADC_PIN_P1_1;
		dInPin = IO_PIN_P1_1;
	}
	else if (port == 2)
	{
		aInPin = ADC_PIN_P2_1;
		dInPin = IO_PIN_P2_1;
	}
	else if (port == 3)
	{
		aInPin = ADC_PIN_P3_1;
		dInPin = IO_PIN_P3_1;
	}
	else
	{
		//Serial.println("--> ERROR");
		//Serial.print("--> Attempting to assign port: ");
	}
	
	switch (deviceID)
	{
		case 0:
			// UNKNOWN Sensor, pass in the ADC Value
			pinMode(aInPin, INPUT);
			sensorValue = analogRead(aInPin);
			return sensorValue;
			
		case 5:
			// Push Button Sensor
			pinMode(dInPin, INPUT);
			sensorValue = digitalRead(dInPin) * 1023;
			if (sensorValue==0)
				nOBJECTS.blinkLED(BLUE_LED_PIN);
			return sensorValue;
		
		case 6:
			// Light Sensor
			pinMode(aInPin, INPUT);
			sensorValue = analogRead(aInPin);
			return sensorValue;

		case 7:
			// PIR Sensor
			pinMode(dInPin, INPUT);
			sensorValue = digitalRead(dInPin) * 1023;
			if (sensorValue>0) nOBJECTS.blinkLED(GREEN_LED_PIN);
			return sensorValue;

		case 8:
			//	DHT22 Humidity Sensor
			myDHT22.setPIN(dInPin);
			errorCode = myDHT22.readData();
			if ((errorCode==DHT_ERROR_NONE) || (errorCode==DHT_ERROR_TOOQUICK))
			{
				sensorValue = myDHT22.getHumidity() * 10 ;			// make a single decimal float to int
				tempValue = myDHT22.getTemperatureC() * 10;		// make a single decimal float to int
				return sensorValue;
			}
			else
				return 0;

		case 9:
			// DHT22 Temperature Sensor
			return tempValue;
		
		case 10:
			// Distance Sensor
			pinMode(aInPin, INPUT);
			sensorValue = analogRead(aInPin);
			return sensorValue;
		
		case 11:
			// 433Mhz Receiver - Not required to return anything
			// Special handling in doPort1()
			return -1;
			
		case 12:
			pinMode(aInPin, INPUT);
			sensorValue = analogRead(aInPin);
			if (sensorValue>0) nOBJECTS.blinkLED(RED_LED_PIN);
			return sensorValue;

		case 1002:
			// Relay Breakout
			pinMode(dInPin, OUTPUT);
			if (port == 1) 
				sensorValue = bitRead(PORTC, 0);
			else if (port == 2)
				sensorValue = bitRead(PORTC, 1);
			else if (port == 3)
				sensorValue = bitRead(PORTC, 2);
			
			return sensorValue;
						
		default:		// Invalid sensor ID
			return -1;
	}
}