Hello,
I'm wondering if anyone has encountered the issue that PWM motor control is not working on the normal PWM pins when also using the audio library at the same time.
I believe it's to do with timers being used for audio that would normally drive the PWM generation.
I'm using a servo and an H-bridge-driven DC-motor controlled by an incoming audio-signal.
Any hints, pointers, help is greatly appreciated
thanks
/*j
here's a MWE:
I'm wondering if anyone has encountered the issue that PWM motor control is not working on the normal PWM pins when also using the audio library at the same time.
I believe it's to do with timers being used for audio that would normally drive the PWM generation.
I'm using a servo and an H-bridge-driven DC-motor controlled by an incoming audio-signal.
Any hints, pointers, help is greatly appreciated
thanks
/*j
here's a MWE:
Code:
#include <Servo.h>
#define AUDIOTHRESH 455
#define AUDIOMAX 505
#define RUNNINGSPEED 255
#define TIMEINTERVAL 250 // should be 1000 for one second inervals if tables are 60 elements long
#define motorCtrl1 6 // analog side PWM
#define motorCtrl2 7 // analog side PWM
#define analogIn1 8 // analog side
#define analogIn2 9 // analog side
#define servoCtrl 5 // digital pin PWM
int startDuration = 8; // seconds
int stopDuration = 8; // seconds
Servo myservo;
int sensorValueL;
int sensorValueR;
int audioValue;
boolean audioState;
int maximum;
int prevMaximum;
unsigned long t0, t1, t2;
unsigned long time1, time2;
int memory[500];
int counter = 0;
boolean motorCounter = 0;
int motorState = 0; // 0 stopped, 1 starting, 2 running, 3 stopping, 4 serching endposition
bool searchStop = false;
void setup()
{
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
myservo.attach(6);
myservo.write(110);
}
void loop()
{
int audioSensingValue;
float accum;
audioSensingValue = analogRead(A0);
audioValue = abs(512 - audioSensingValue);
// --- windowed audio, collecting maximum value
memory[counter++] = audioValue;
if(counter >= 500 ) {
counter = 0;
}
maximum = -10000;
for(int i = 0; i < 500; i++)
{
if(memory[i] >= maximum) { maximum = memory[i]; }
}
// logic for audio sync
if(maximum > AUDIOTHRESH)
{
if(maximum >= AUDIOMAX){
motorState = 2;
}else{
motorState = 1;
if(audioState == 0)
{
myservo.write(110);
audioState = 1;
}
}
}
if(maximum <= AUDIOTHRESH)
{
if(audioState == 1) // transition to low
{
motorState = 4; // search locking position
}
}
// --- motor control
time1 = millis();
if(time1 >= time2 + TIMEINTERVAL ) // do everything here in 1 second intervals
{
if(motorState == 1) // starting
{
analogWrite(motorCtrl, maximum );
myservo.write(110);
searchStop = false;
}
else if (motorState == 2) // running
{
analogWrite(motorCtrl, 255);
}
else if (motorState == 3) // stopping
{
}
else if(motorState == 0) // stopped
{
analogWrite(motorCtrl, 0);
motorCounter = 0;
}
time2 = time1;
}
}
Last edited: