Skip to content

Instantly share code, notes, and snippets.

@greggjaskiewicz
Created September 27, 2022 08:56
Show Gist options
  • Save greggjaskiewicz/923cfb2fbcbd4feb4ed2ff832b1aae3f to your computer and use it in GitHub Desktop.
Save greggjaskiewicz/923cfb2fbcbd4feb4ed2ff832b1aae3f to your computer and use it in GitHub Desktop.
Kitt RP2040
// uses my PWM library
#include <stdio.h>
#include <vector>
#include <map>
#include "pico/stdlib.h"
static std::vector<uint8_t> pwmValues;
static std::vector<PiPWM *> pwms;
static int current = 0;
static int direction = 1;
bool timer_callback(struct repeating_timer *t) {
if (current == 0) {
direction = 1;
}
if (current == pwms.size()-1) {
direction = -1;
}
for(int x=0; x<pwms.size(); x++) {
if (pwmValues[x] == 0) {
continue;
}
pwmValues[x] /= 3;
pwms[x]->setDuty(pwmValues[x]);
}
current += direction;
pwmValues[current] = 100;
pwms[current]->setDuty(100);
return true;
}
int main() {
struct repeating_timer timer;
for(int pin=2; pin<14; pin++) {
PiPWM *pwm = new PiPWM(pin);
pwm->start();
pwms.push_back(pwm);
pwmValues.push_back(0);
}
bool timerAdded = add_repeating_timer_ms(-50, timer_callback, NULL, &timer);
assert(timerAdded == true);
while (1)
tight_loop_contents();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment