Skip to content

Instantly share code, notes, and snippets.

@eyllanesc
Last active November 30, 2017 18:34
Show Gist options
  • Save eyllanesc/4437a6140aad16421ddfa9f4c68a8bfd to your computer and use it in GitHub Desktop.
Save eyllanesc/4437a6140aad16421ddfa9f4c68a8bfd to your computer and use it in GitHub Desktop.
#include <DuePWM.h>
#define PWM_FREQ1 20
#define PWM_FREQ2 20
// DuePWM can be instantiated using a default constrcutor like this:
// DuePWM pwm();
// OR
// DuePWM pwm;
// In these cases it will use the default values of PWM_FREQUENCY
// defined in variant.h which is currently 1kHz and is equivalent to:
// DuePWM pwm(PWM_FREQUENCY, PWM_FREQUENCY);
DuePWM pwm( PWM_FREQ1, PWM_FREQ2 );
uint32_t pinl = 6;
uint32_t pinr = 7;
uint32_t current;
int val = 0;
int interruptPin = 2;
void setup()
{
Serial.begin(9600);
pwm.setFreq1( PWM_FREQ1 );
pwm.setFreq2( PWM_FREQ2 );
pwm.pinFreq1( pinl );
pwm.pinFreq2( pinr );
//pwm.stop(pinr);
delay(1000);
current = pinl;
attachInterrupt(digitalPinToInterrupt(interruptPin), onInterrupt, CHANGE);
}
void loop()
{
int tmp = analogRead(A0)/4;
if(tmp < 10)
tmp = 0;
if(tmp > 250)
tmp = 250;
if(tmp != val){
val = tmp;
pwm.pinDuty(current, val);
}
delay(100);
Serial.print(current);
Serial.print(" ");
Serial.println(val);
}
void onInterrupt(){
pwm.pinDuty(current, 0);
current = digitalRead(interruptPin) == HIGH ? pinl : pinr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment