Pages: [1]   Go Down
Author Topic: Analog Read  (Read 539 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;}
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 99
Posts: 3618
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Queens, New York
Offline Offline
Faraday Member
**
Karma: 99
Posts: 3618
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: