Go Down

Topic: jerky mapped values from sensor (Read 983 times) previous topic - next topic

danieljay

so I've got a sensor that is mapped to lead's. as i transition into the next range of my mapped values, the sensor readings flicker, causing the LED to glitch out a bit. heres my code:

Code: [Select]


int ledVal;

const int pingPin = 7;
const int ledG1 = 11;
const int ledY1 = 10;
const int ledR1 = 9;






int outputValue1;
int outputValue2;
int outputValue3;
int outputValue4;


void setup() {

  Serial.begin(9600);
  pinMode(ledG1, OUTPUT);

  pinMode(ledR1, OUTPUT);

  pinMode(ledY1, OUTPUT);

}

void loop()
{

  long duration, inches, cm;

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  // delay(5);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  //
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  //delay(10);

  int ledVal = cm;




  for(int value = 0; value <=0; value +=15)                             // ramp the number in "value" from 0 to 255
  {


    //forwrd fade


    outputValue1 = map(ledVal,28, 37, value, 255);


    outputValue2 = map(ledVal, 34, 23, value, 255);

    //delay(100);
    outputValue3 = map(ledVal, 15, 23, value, 255);

    //delay(100);
    outputValue4= map(ledVal, 17, 2, value, 255);

  }


  if(ledVal <=37 && ledVal >= 28){

    analogWrite(ledG1, outputValue1);
    digitalWrite(ledR1, LOW);
  }
  if(ledVal <=34 && ledVal >=23 ){

    analogWrite(ledY1, outputValue2 );

    digitalWrite(ledR1, LOW);

  }

  if(ledVal <= 23 && ledVal >=15)
  {
    analogWrite(ledY1, outputValue3);
    digitalWrite(ledG1, LOW);

    if(ledVal <= 17 && ledVal >=2)
      analogWrite(ledR1, outputValue4 );

    digitalWrite(ledG1, LOW);

  }}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}


sirbow2

try reading the value a few (30?) times with a small delay between each reading and adding each reading to a integer, then divide that integer by 30 to get a average.
http://dduino.blogspot.com all my Arduino/electronic projects!!!

{NEW} Getting Started, Learning, Reference + FAQ PDF!!:
[url]http://ar

danieljay

something like this?

Code: [Select]
int ledVal = cm;
int avRead = comp + ledVal/30;

danieljay

and I've tried it and i still get inconsistent values at certain points. usually the points that transition from my mapped values.

sirbow2

#4
Apr 05, 2012, 01:12 am Last Edit: Apr 05, 2012, 01:13 am by sirbow2 Reason: 1
Code: [Select]
int sensorvalue =0;
for(int i = 0; i<=30; i++)
{
  sensorvalue += [the just read sensor value]; //could be, for example,  sensorvalue += analogRead(somepin);
  delay(10); //find a good value
}
sensorvalue = sensorvalue/30;
http://dduino.blogspot.com all my Arduino/electronic projects!!!

{NEW} Getting Started, Learning, Reference + FAQ PDF!!:
[url]http://ar

danieljay

and i know this isn't solving my problem. that sounds like its just dividing the outputted values into smaller increments.


sirbow2

its called AVERAGE
instead of taking one reading, it takes 30 readings and then divides by 30 to get the average of the 30 values...
http://dduino.blogspot.com all my Arduino/electronic projects!!!

{NEW} Getting Started, Learning, Reference + FAQ PDF!!:
[url]http://ar

PaulS

It isn't solving your problem because you sill don't have a clue what map does. WHERE ARE YOUR SERIAL PRINT STATEMENTS?

MarkT

If you could explain what all those random-looking numbers in the calls to map are supposed to mean, it might help us understand what you are trying to do.
[ I won't respond to messages, use the forum please ]

danieljay

those random numbers is exactly what is written, without doing any math. I'm dividing the sensor values by 4 less, and triggering them within the range.

danieljay

and i have an idea how to use map. if their is a more effective way to obtain the same effect, constructive feedback is welcomed.

PaulS

Quote
and i have an idea how to use map.

Prove it! Put some Serial.print() statements in your code, and show the output!

danieljay

well I've had some changes in the code not using a map and just keeping the if/then statements with the same problems. so, as I serial printed the sensor values, as i have, the sensor values become jerky as I'm switching into the if/then statements. and I'm aware of this due to the serial print numbers fluctuating between the two numbers in-between the if statements. so that leads me i guess to my original question:

Quote
so I've got a sensor that is mapped to lead's. as i transition into the next range of my mapped values, the sensor readings flicker, causing the LED to glitch out a bit.


so replace the word, "mapped" with my new, but consistent problem of this happening between if statements.

PaulS

Quote
well I've had some changes in the code

Here's a clue by four. Whack yourself with it until you figure out that we can't see the changes you make unless you post the code. Not too hard, though.

Go Up