Go Down

Topic: Analog Read (Read 585 times) previous topic - next topic

Cummins

I have been having trouble figuring out how to write the code to read a total amount drop in an analog signal. My sensor is 0-5volts and it works from 74-718 (0-100% position). I use smoothing normally but i took it out of my example code to get to my point.  I am trying to figure out how to read a specific amount of drop in voltage within a specific amount of time to light an led. Both voltage drop and time are variables cause it will get tuned in once I can get it to actually work. If anyone could direct me to a better way of doing this and or how I should go about figuring it out it would be very helpful. Thank you for your time
Code: [Select]

int SET, FALL=180, deb=30;
unsigned long cm,pm,pm1,pm2,pm3;

int LED=13;

void setup() {
   pinMode(A0, INPUT);
   pinMode(LED, OUTPUT);
   
}
void loop() {
  int APPS=analogRead(A0);
  cm=millis();
 
  if(APPS>SET){SET=APPS;pm=0;pm1=0;pm2=0;}
    if(APPS<SET){pm1=cm;}else{SET=APPS;pm1=0;}
    if(cm-pm1<deb){}else{
      if(APPS<SET-FALL){pm2=cm;}else{SET=APPS;pm1=0;}}
    if(cm-pm2<deb){}else{
      if(APPS<SET-FALL){digitalWrite(LED, HIGH);pm=cm;SET=285;}else{SET=APPS;pm=0;pm1=0;pm2=0;}}
  if(cm-pm<250){}else{digitalWrite(LED, LOW);pm=0;}

HazardsMind

Please separate your code, the arduino software has a format option, please repost your code in the proper format. At lease so we can see what is going on.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

PeterH

The code is incomprehensible.

I suggest you put each { and } on separate lines with matching pairs indented to the same level and the code between them indented one extra level.

Choose meaningful variable names.  SET, FALL, deb, cm, pm, pm1, pm2, pm3 mean nothing to me, and there aren't even any comments to give a hint.

The requirement to detect a specified drop in voltage in a specified time could actually be very hard to implement completely, depending what you mean by that. Given a signal value which is changing continuously over time, possibly in an erratic manner, under exactly what conditions do you want your condition to trigger?
I only provide help via the forum - please do not contact me for private consultancy.

HazardsMind

What you can try is to get two data points, store the first one and compare it to the second one after a certain amount of time (couple of mills), you also may want to give the second variable some slack. When the second variable drops or rise beyond the first variable + the slack, you can trigger the LED to respond.

Codewise it should not be very hard to do, if of course you have some idea as to how to do it.
Try it, if anything else another member will have another idea to try.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Cummins

Thanks hazards thats basically what that code does. I thought I explained it alright so ill just figure it out on my own. Thanks for the replies

Go Up