Question: The below code uses an Arduino and an EMG sensor to light up an LED when a muscle is flexed MODIFY THE BELOW CODE TO

The below code uses an Arduino and an EMG sensor to light up an LED when a muscle is flexed

MODIFY THE BELOW CODE TO MAKE A SEPERATE LED LIGHT UP WHEN THE MUSCLE IS NOT FLEXED

//EMG variables

int threshold = 15;

int count = 0;

int sensorSum = 0;

const int interval = 10;

int average =0;

const int sensorPin = A0;

const int ledPin = 9;

//calibration variables

int sensorValue = 0;

int sensorMin = 1023;

int sensorMax = 0;

int sensorAvg = 0;

int pulseValue = 0;

//potentiometer variables

int const potPin = A1;

int potVal = 0;

void setup() {

// put your setup code here, to run once:

Serial.begin(115200);

pinMode(ledPin, OUTPUT); //set pin 9 to be an output pin

digitalWrite(ledPin, LOW); //turn Yellow LED off

pinMode(13, OUTPUT); //set onboard LED to be an output pin

digitalWrite(13, HIGH); //turn onboard LED on

delay(50);

//run calibration sequence

while(millis() < 5000){

sensorValue = analogRead(sensorPin);

if (sensorValue > sensorMax) {

sensorMax = sensorValue;

}

if (sensorValue < sensorMin){

sensorMin = sensorValue;

}

sensorAvg = (sensorMax + sensorMin)/2;

}

digitalWrite(13, LOW); //turn onboard LED off to indicate calibration period end

}

void loop() {

// put your main code here, to run repeatedly:

//potentiometer sets value of threshold

potVal =analogRead(potPin);

threshold = potVal*0.05;

//take reading from EMG sensor

sensorValue = analogRead(sensorPin);

//compare to average from calibration

sensorValue = abs(sensorValue - sensorAvg);

//cumulative sum of sensor readings

sensorSum = sensorValue + sensorSum;

//advance count variable by 1

count++;

//check if count variable is up to 10,

//if so, average last 10 sensor readings

if(count == interval){

average = sensorSum/count;

//check if average is higher than threshold

//if so, turn on LED and collect pulse

if(average >= threshold){

digitalWrite(ledPin, HIGH);

pulseValue = threshold;

}

//if average is not higher than threshold

//turn LED off and set pulse to zero

else{

digitalWrite(9, LOW);

pulseValue = 0;

}

//print data to serial monitor

Serial.print(threshold); //threshold set by potentiometer

Serial.print(", ");

Serial.print(average); //sensor reading average

Serial.print(", ");

Serial.println(pulseValue); //pulse

sensorSum = 0; //reset sensorSum

count =0; //reset count

}

delay (5); //wait 5 ms for stability

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!