SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By skatun
#50008
I want to print out the angle in degreese, like 43.1 from this module http://www.sparkfun.com/commerce/produc ... cts_id=761

at the moment it prints out this rotating an whole round when its connected to 5v:



Direction 1: 75

Direction 2: 113

Direction 1: 76

Direction 2: 116

Direction 1: 76

Direction 2: 116

Direction 1: 76

Direction 2: 112

Direction 1: 74

Direction 2: 114

Direction 1: 77

Direction 2: 110

Direction 1: 76

Direction 2: 116

Direction 1: 77

Direction 2: 117

Direction 1: 77

Direction 2: 117

Direction 1: 77

Direction 2: 117

Direction 1: 77

Direction 2: 117

Direction 1: 77

Direction 2: 117

Direction 1: 77

Direction 2: 117

Direction 1: 77

Direction 2: 117

Direction 1: 77

Direction 2: 117

3volt gives this

Direction 1: 312

Direction 2: 300

Direction 1: 312

Direction 2: 300

Direction 1: 312

Direction 2: 300

Direction 1: 311

Direction 2: 300

Direction 1: 311

Direction 2: 300


Code: Select all
/*
 * Smoothing
 * David A. Mellis <dam@mellis.org>
 *
 * Reads repeatedly from an analog input, calculating a running average
 * and printing it to the computer. 
 *
 * http://www.arduino.cc/en/Tutorial/Smoothing
 */

// Define the number of samples to keep track of.  The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input.  Using a #define rather than a normal variable lets
// use this value to determine the size of the readings array.
#define NUMREADINGS 10
#define dir1  0
#define dir2  1


long previousMillis = 0;	  // will store last time LED was updated
long interval = 3000;	     // interval at which to blink (milliseconds)




int readings[NUMREADINGS][5];		    // the readings from the analog input
int index = 0;				    // the index of the current reading

int total1,total2,direction1,direction2;



void setup()
{
  Serial.begin(9600); 
  Serial.println("No stratet serieporten");     // initialize serial communication with computer
 pinMode(2, OUTPUT); 
 digitalWrite(2,HIGH);
 delay(2000);
 digitalWrite(2,LOW);
 total1 = 0;
 total2 = 0;
}

void loop()
{

  total1 +=analogRead(dir1);  // add the reading to the total
  total2 += analogRead(dir2); // add the reading to the total
  


  index++;			  // advance to the next index

  if (index >= NUMREADINGS)   {		// if we're at the end of the array...
    index = 0;				    // ...wrap around to the beginning
   
    direction1 =  total1 / NUMREADINGS;	    // calculate the average
    direction2 =  total2 / NUMREADINGS;	    // calculate the average
    
    total1 = 0;
    total2 = 0;
    
  }

  if (millis() - previousMillis > interval) {
    previousMillis = millis();		 // remember the last time we blinked the LED
    Serial.print("Direction 1: "); 
    Serial.println(direction1);		   // send it to the computer (as ASCII digits)
    
    Serial.print("Direction 2: "); 
    Serial.println(direction2);		   // send it to the computer (as ASCII digits)
    
  }
}