Question: I am trying to modify an existing code that will turn on an LED when I flex my muscle to instead turn on a motor.
I am trying to modify an existing code that will turn on an LED when I flex my muscle to instead turn on a motor. I have an Arduino hooked up to a Bitalino EMG sensor. I have attached photos of the set up as well as included the old code which turned on the LED and the code that I thought would turn the motor, but it is not working. The transistor is going into Pin 9 of the Arduino.
LED CODE:
//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()
sensorValue = analogRead(sensorPin);
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
if (sensorValue
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
}

MY ATTEMPT AT THE MOTOR CODE:
//EMG variables int threshold = 15; int count = 0; int sensorSum = 0; const int interval = 10; int average =0; const int sensorPin = A0; const int motorPin = 9; int switchState = 0;
//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(motorPin, OUTPUT); //set pin 9 to be an output pin digitalWrite(motorPin, LOW); //turn Yellow LED off
pinMode(9, OUTPUT); //set onboard LED to be an output pin digitalWrite(9, HIGH); //turn onboard LED on
delay(50);
//run calibration sequence while(millis() sensorMax) { sensorMax = sensorValue; } if (sensorValue
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(motorPin, 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
Get step-by-step solutions from verified subject matter experts
