I built it in a simulator here, its a free account so you can probably play with it there too:
https://wokwi.com/projects/429266389894726657
It was a new tool to me, so I've only got 30 mins experience with it. I could figure out how to get debug output, though with a bit of work you should be able to get println data in a console and be able to use that for basic debugging more easily.
I added some extra LEDs to monitor the state of done, halfdone and math. It looks like its getting stuck every iteration.
Then I realized, I don't think there is any code to read a timer so currentmillis is always 0 and this is why the logic doesn't iterate and there is no output. I added an incrementor to current micros at the bottom but its still not enough.
currentmicros+=1000; // just to make it tick, not a real clock.
Also sequence[tooth]; and cam[tooth]; don't do anything, they resolve to values but then nothing is done with them.
I modified this line: if (sequence[tooth] == 0) to add the [tooth] so it reads the pattern from the array instead of just whatever sequence without the offset is (default offset 0? pointer? never even tried this..).
but I left the 2 that do nothing above as I don't know your intention there.
I updated those 2 lines at code, havnt looked at how to read a timer or what type of timers are available yet.
I was also unsure if you can use pin1 and pin2 as they also connected tot he UART, so I moved them to the GPIOs at the other end of the board.
I moved the logic that sets sequence (and added cam) to the top of the main loop. I think its clearer to group that functionality together and just re-set them all each loop. Personal preference design choice, though.
I also fined the pins to make it easier to see what is what and change them around if the hardware does need further adjustments.
I'll reproduce my version of the code here incase someone messes up the code in the link above.
Its not fixed, but hope it give you some things to think about and an easy way in to that simulator which might help you get it figured.
If you put a delay(500); as the end of the main loop you can see the output leds blinking at a speed visible to the human eye.
Code: Select all
#include <Arduino.h>
#define PIN_HALFDONE 4
#define PIN_DONE 5
#define PIN_MATH 6
#define PIN_SEQUENCE 14
#define PIN_CAM 15
#define PIN_INPUT A0
int TPS = 0;
int sequence[48] = {0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0};
int cam[48] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int tooth = 0;
int low = 0;
int high = 0;
int RPMlow = 0;
int RPMhigh = 0;
int math = 1;
int halfdone = 0;
int done = 1;
int currentmicros = 0;
int previousmicros = 0;
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("Hello from the Pico!"); // Not working. But would be nice to have println debugging...
pinMode(PIN_INPUT, INPUT);
pinMode(PIN_SEQUENCE, OUTPUT);
pinMode(PIN_CAM, OUTPUT);
pinMode(PIN_HALFDONE, OUTPUT);
pinMode(PIN_DONE, OUTPUT);
pinMode(PIN_MATH, OUTPUT);
}
void loop() {
// update outputs
digitalWrite(PIN_HALFDONE, halfdone);
digitalWrite(PIN_DONE, done);
digitalWrite(PIN_MATH, math);
digitalWrite(PIN_SEQUENCE, sequence[tooth]);
digitalWrite(PIN_CAM, cam[tooth]);
if (halfdone == 0) {
if (currentmicros - previousmicros >= RPMlow) {
halfdone = 1;
done = 0;
previousmicros = currentmicros;
}
}
if (done == 0) {
if (currentmicros - previousmicros >= RPMhigh) {
done = 1;
math = 1;
previousmicros = currentmicros;
}
}
// increment output state logic
if (math == 1) {
tooth++;
if (tooth == 48) {
tooth = 0;
}
TPS = analogRead(A0);
sequence[tooth];
cam[tooth];
if (sequence[tooth] == 0) {
low = 1;
high = 2;
}
else {
low = 2;
high = 1;
}
halfdone = 0;
math = 0;
RPMlow = low * TPS;
RPMhigh = high * TPS;
//RPMlow = 100;
//RPMhigh = 100;
currentmicros+=1000; // just to make it tick, not a real clock.
}
}
Code: Select all
{
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-pi-pico",
"id": "pico",
"top": 73.65,
"left": 13.2,
"attrs": { "env": "arduino-community" }
},
{ "type": "wokwi-vcc", "id": "vcc1", "top": 77.56, "left": 172.8, "attrs": {} },
{ "type": "wokwi-gnd", "id": "gnd1", "top": 19.2, "left": 172.2, "attrs": {} },
{ "type": "wokwi-potentiometer", "id": "pot1", "top": 181.1, "left": 143.8, "attrs": {} },
{
"type": "wokwi-led",
"id": "led3",
"top": 92.4,
"left": -236.2,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led5",
"top": 207.6,
"left": -236.2,
"attrs": { "color": "limegreen" }
},
{
"type": "wokwi-led",
"id": "led4",
"top": 150,
"left": -236.2,
"attrs": { "color": "yellow" }
},
{
"type": "wokwi-text",
"id": "text1",
"top": 105.6,
"left": -201.6,
"attrs": { "text": "halfdone" }
},
{
"type": "wokwi-text",
"id": "text2",
"top": 163.2,
"left": -201.6,
"attrs": { "text": "done" }
},
{
"type": "wokwi-text",
"id": "text3",
"top": 220.8,
"left": -201.6,
"attrs": { "text": "math" }
},
{
"type": "wokwi-text",
"id": "text4",
"top": 278.4,
"left": -201.6,
"attrs": { "text": "sequence" }
},
{
"type": "wokwi-text",
"id": "text5",
"top": 326.4,
"left": -201.6,
"attrs": { "text": "cam" }
},
{
"type": "wokwi-led",
"id": "led1",
"top": 313.2,
"left": -236.2,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-led",
"id": "led2",
"top": 265.2,
"left": -236.2,
"attrs": { "color": "cyan" }
}
],
"connections": [
[ "pico:GP0", "$serialMonitor:RX", "", [] ],
[ "pico:GP1", "$serialMonitor:TX", "", [] ],
[ "pico:GND.8", "gnd1:GND", "black", [ "v-38.4", "h116.4", "v-48" ] ],
[ "pico:GP26", "pot1:SIG", "green", [ "h30", "v96", "h67.6" ] ],
[ "vcc1:VCC", "pot1:VCC", "red", [ "v19.2", "h57.6", "v144", "h-47.2" ] ],
[ "pot1:GND", "gnd1:GND", "black", [ "v38.4", "h86.4", "v-268.8" ] ],
[ "gnd1:GND", "led3:C", "black", [ "v0", "h-432", "v115.2" ] ],
[ "gnd1:GND", "led5:C", "black", [ "v0", "h-432", "v230.4" ] ],
[ "vcc1:VCC", "pico:3V3", "red", [ "v0" ] ],
[ "vcc1:VCC", "pico:ADC_VREF", "red", [ "v0" ] ],
[ "led5:A", "pico:GP6", "green", [ "h172.8", "v-76.8", "h0", "v-9.6" ] ],
[ "pico:GP5", "led4:A", "green", [ "h-76.8", "v48", "h-153.6" ] ],
[ "pico:GP4", "led3:A", "green", [ "h0" ] ],
[ "gnd1:GND", "led4:C", "black", [ "v0", "h-432", "v172.8" ] ],
[ "pico:GP15", "led1:A", "green", [ "h-28.8", "v86.4" ] ],
[ "gnd1:GND", "led1:C", "black", [ "v0", "h-432", "v336" ] ],
[ "pico:GP14", "led2:A", "green", [ "h-48", "v48" ] ],
[ "gnd1:GND", "led2:C", "black", [ "v0", "h-432", "v288" ] ]
],
"dependencies": {}
}