Skip to content

Commit

Permalink
Extend ADC Plugin with multiplex and powersave
Browse files Browse the repository at this point in the history
If none of the 2 GPIO pins is set it works as usual and the value get
written in "Analog1". If one or both GPIO pins are set the pin is set to
HIGH while reading A0. This way 2 analog sensors are possible. The
positive source for the sensor is 1st GPIO for Analog1 and 2nd GPIO for
Analog2. It's also possible to set only one of the both GPIO. In this
case it works like a powersave because the sensor only get voltage for
the short moment of measure.
  • Loading branch information
Tommy-LSA committed Dec 29, 2016
1 parent af48467 commit 4d6f1c3
Showing 1 changed file with 54 additions and 11 deletions.
65 changes: 54 additions & 11 deletions _P002_ADC.ino
Expand Up @@ -5,7 +5,8 @@
#define PLUGIN_002
#define PLUGIN_ID_002 2
#define PLUGIN_NAME_002 "Analog input"
#define PLUGIN_VALUENAME1_002 "Analog"
#define PLUGIN_VALUENAME1_002 "Analog1"
#define PLUGIN_VALUENAME2_002 "Analog2"
boolean Plugin_002(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
Expand All @@ -16,13 +17,13 @@ boolean Plugin_002(byte function, struct EventStruct *event, String& string)
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_002;
Device[deviceCount].Type = DEVICE_TYPE_ANALOG;
Device[deviceCount].VType = SENSOR_TYPE_SINGLE;
Device[deviceCount].Type = DEVICE_TYPE_DUAL;
Device[deviceCount].VType = SENSOR_TYPE_DUAL;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 1;
Device[deviceCount].ValueCount = 2;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
Expand All @@ -38,18 +39,60 @@ boolean Plugin_002(byte function, struct EventStruct *event, String& string)
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_002));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_002));
break;
}

case PLUGIN_READ:
{
int value = analogRead(A0);
UserVar[event->BaseVarIndex] = (float)value;
String log = F("ADC : Analog value: ");
log += value;
addLog(LOG_LEVEL_INFO,log);
success = true;
break;
int value;
if (Settings.TaskDevicePin1[event->TaskIndex] == -1 && Settings.TaskDevicePin2[event->TaskIndex] == -1)
{
value = analogRead(A0);
UserVar[event->BaseVarIndex] = (float)value;
String log = F("ADC : Analog value: ");
log += value;
addLog(LOG_LEVEL_INFO,log);
UserVar[event->BaseVarIndex+1] = 0;
success = true;
break;
}
else
{
if(Settings.TaskDevicePin1[event->TaskIndex] > -1)
{
pinMode(Settings.TaskDevicePin1[event->TaskIndex], OUTPUT);
digitalWrite(Settings.TaskDevicePin1[event->TaskIndex], 1);
value = analogRead(A0);
digitalWrite(Settings.TaskDevicePin1[event->TaskIndex], 0);
UserVar[event->BaseVarIndex] = (float)value;
String log = F("ADC : Analog value1: ");
log += value;
addLog(LOG_LEVEL_INFO,log);
}
else
{
UserVar[event->BaseVarIndex] = 0;
}

if(Settings.TaskDevicePin2[event->TaskIndex] > -1)
{
pinMode(Settings.TaskDevicePin2[event->TaskIndex], OUTPUT);
digitalWrite(Settings.TaskDevicePin2[event->TaskIndex], 1);
value = analogRead(A0);
digitalWrite(Settings.TaskDevicePin2[event->TaskIndex], 0);
UserVar[event->BaseVarIndex+1] = (float)value;
String log = F("ADC : Analog value2: ");
log += value;
addLog(LOG_LEVEL_INFO,log);
}
else
{
UserVar[event->BaseVarIndex+1] = 0;
}
success = true;
break;
}
}
}
return success;
Expand Down

0 comments on commit 4d6f1c3

Please sign in to comment.