Pages: [1]   Go Down
Author Topic: jerky mapped values from sensor  (Read 835 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 126
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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

Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 486
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

http://dduino.blogspot.com all my Arduino/electronic projects!!!

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

Offline Offline
Full Member
***
Karma: 0
Posts: 126
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

something like this?

Code:
int ledVal = cm;
int avRead = comp + ledVal/30;
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 126
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Sr. Member
****
Karma: 1
Posts: 486
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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;
« Last Edit: April 04, 2012, 06:13:44 pm by sirbow2 » Logged

http://dduino.blogspot.com all my Arduino/electronic projects!!!

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

Offline Offline
Full Member
***
Karma: 0
Posts: 126
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 486
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

http://dduino.blogspot.com all my Arduino/electronic projects!!!

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11694
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

[ I won't respond to messages, use the forum please ]

Offline Offline
Full Member
***
Karma: 0
Posts: 126
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 0
Posts: 126
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and i have an idea how to use map.
Prove it! Put some Serial.print() statements in your code, and show the output!
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 126
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: