Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You have a plan to build a games console controller that captures the hand gestu

ID: 2292555 • Letter: Y

Question

You have a plan to build a games console controller that captures the hand gestures of the player. The player’s movements and gestures are sensed using an accelerometer connected to one of the ADC pins of an Arduino. You have already written all the necessary code but have yet to include any kind of smoothing for the accelerometer signal.

Your task now is to write the accelerometer smoothing function. The function must take each new sample as input and return the average of 4 consecutive samples without introducing a significant lag. (Hint: use a moving average.)

Write an implementation of this function where the function prototype is of the form:

int SmoothSensor(int sensorReading);

Explanation / Answer

I modified the code, to implement moving average in the form of function. Given below is the complete code, I have verified this code

const int numReadings = 4;

int readings[numReadings];      // for readings from the analog input
int readIndex = 0;              // it is the index of the current reading
int total = 0;                  // the running total of moving average
int average = 0;                // initial value of the average

int inputPin = A0; // sensor is attached to pin A0

void setup() {
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
}
}

void loop() {
SmoothSensor ();
delay(1);        // delay in between reads for stability
}

int SmoothSensor (){

   // subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = analogRead(inputPin);
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;

// if we're at the end of the array...
if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
}

// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.println(average);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote