Go Down

Topic: How to collect a data and compare it to another two values (Read 682 times) previous topic - next topic

achen29

Hi,
 I want to use A4 port to be input and collect data. And then compare the data with thresholdlow and thresholdhigh and then to generate the output voltage by A0. There is some problem with my program. You can see from the attached PNG, the upper window shows the data collected by A4. The data is smaller than thresholdhigh and bigger than thresholdlow, and it should be zero, however, it is not what i expected. Does anyone have some advice? I really appreciate it !




Code: [Select]

/*
  Conditionals - If statement

 
// These constants won't change:
const int analogPin = A4;    // pin that the sensor is attached to
const int DACPin = A0;       // pin that the LED is attached to
const int thresholdlow = 760;   // an arbitrary threshold level that's in the range of the analog input
const int thresholdhigh = 791;
int val=0;

void setup() {
  // initialize the LED pin as an output:
  //pinMode(DACPin, OUTPUT);
  // initialize serial communications:
  //Serial.begin(9600);
  Serial.begin(115200);
  analogWriteResolution(10);
  analogReadResolution(10);
}

void loop() {
  // read the value of the potentiometer:
 val = analogRead(analogPin);
 
  // if the analog value is high enough, turn on the LED:
  if ((val<=thresholdlow )||(val>=thresholdhigh)) {
   
      analogWrite(DACPin,93);
  } else  {
       analogWrite(DACPin,0);
  }

  // print the analog value:
  Serial.println(val);
  //delay(1);        // delay in between reads for stability
}

AloyseTech

You shouldn't drive a led with the DAC directly. Could you share you schematic? I can't tell if the issue is noise or the schematic for example.

I see no issue with your code :

if voltage on pin A4 is between 2.45V and 2.55V, the DAC outputs 0.3V, else, it outputs 0V.

Go Up