Moving average on analog input
Description
This small project shows how to implement a moving average with a lightweight form in order to smooth raw analog values.
The value is then used to position a servo
Image
code Arduino:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
AnalogReadSerial | |
Reads an analog input on pin 0, prints the result to the Serial Monitor. | |
Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu). | |
(Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.) | |
creates a moving average of the analog value read | |
positions the servo according to the averaged value read | |
*/ | |
#include <Servo.h> | |
Servo myservo; // create servo object to control a servo | |
int val; // variable to send servo position | |
int k = 32; | |
int analog_av = 0; | |
int analog_hist[32]; | |
int i = 0; | |
// the setup routine runs once when you press reset: | |
void setup() { | |
// initialize serial communication at 9600 bits per second: | |
Serial.begin(9600); | |
myservo.attach(9); // attaches the servo on pin 9 to the servo object | |
val = 0; | |
for (int n = 0; n < k; n++) { | |
analog_hist[n] = 0; | |
} | |
analog_av = 0; | |
} | |
// the loop routine runs over and over again forever: | |
void loop() { | |
// read the input on analog pin 0: | |
int sensorValue = analogRead(A0); | |
//compute mobile average | |
int sensor_divided = sensorValue / k; | |
analog_av = analog_av + sensor_divided - analog_hist[i]; | |
analog_hist[i] = sensor_divided; | |
i ++; | |
if (i == k) { | |
i = 0; | |
} | |
delay(100); // delay in between reads for stability | |
// print out the value you read: | |
char disp; | |
disp = sensorValue + " / " + analog_av; | |
//drive servo | |
val = map(analog_av, 600, 700, 140, 0); | |
val = max(0, val); | |
val = min(140, val); | |
myservo.write(val);// sets the servo position according to the scaled value | |
delay(25); // waits for the servo to get there | |
Serial.println(val); | |
} |
Using Ultrasonic sensor
Description
In this project, we are using a US sensor. Very first step to understand the behaviour of the sensor through the serial interface.
Image
code Arduino:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Ultrasonic Sensor HC-SR04 and Arduino* | |
*/ | |
// defines pins numbers | |
const int trigPin = 9; | |
const int echoPin = 10; | |
// defines variables | |
long duration; | |
int distance; | |
void setup() { | |
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output | |
pinMode(echoPin, INPUT); // Sets the echoPin as an Input | |
Serial.begin(9600); // Starts the serial communication | |
} | |
void loop() { | |
// Clears the trigPin | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
// Sets the trigPin on HIGH state for 10 micro seconds | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
// Reads the echoPin, returns the sound wave travel time in microseconds | |
duration = pulseIn(echoPin, HIGH,50000); | |
// Calculating the distance | |
distance= duration*0.034/2; | |
// Prints the distance on the Serial Monitor | |
Serial.print("Distance: "); | |
Serial.println(distance); | |
} |
Comments
Post a Comment