/*
* LEDsPotentiometerRPico
* Code by: Gray Mack
* Published at: https://wokwi.com/projects/369035516484149249
* Description:
* test of arduino sketch on r pi pico
* button controls the response time
* potentiometer controls the LED count in counter clockwise
* Based on Jonathan James post in Wokwi Users Group - Arduino, ESP32 and Embedded System Simulator
* https://wokwi.com/projects/368717549372526593?fbclid=IwAR3EX3-7A2cF3i8adi3nVjZpkO2F7bCa7jWNGvrqi9f_BLWUilizRi0qv6c
* I wanted to learn more aobut pico specifics so to rewrite the python example in arduino c++
*
* License: MIT License
* https://choosealicense.com/licenses/mit/
* Created: 7/1/2023
* Board: Raspberry Pi Pico
* Select Board: Pico
* Processor: RP2040
* Gpio: A maximum of 16mA per pin with the total current from all pins not exceeding 51mA
* so 1000ohm resistors were chosen to keep current low
*
* Connections:
* button on GP28 to ground
* Pot on GP26
* All other IO pins to LEDs
*
*
* 7/1/2023 initial code creation
*/
// ----[ included libraries ]--------------------------------
#include "Button2.h"
// ----[ configuration ]------------------------------------
#define SerialBaud 9600
// ----[ pin definitions ]-----------------------------------
const int8_t LED_COUNT = 24;
const int8_t PIN_LEDS[LED_COUNT] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 27 };
const int8_t PIN_BUTTON = 28;
const int8_t PIN_POT = 26;
// ----[ constants ]-----------------------------------------
const int ANALOG_RES = 12;
const int ANALOG_MAX_VAL = 4095; // (2^12-1);
const int DELAY_TIME = 40;
const int MaxButtonCheckTime = 1000;
// ----[ global variables ]----------------------------------
int TotalDelayTime = 1;
int PotValue = 0;
int LedValueOld = 0;
int LedValue = 0;
Button2 ButtonSpeedChange;
// ----[ code ]----------------------------------------------
byte myButtonStateHandler() {
return digitalRead(PIN_BUTTON);
}
void myTapHandler(Button2 &b) {
Serial.print("Button pushed, interval=");
TotalDelayTime+=2;
if(TotalDelayTime*DELAY_TIME>1000) TotalDelayTime=1;
Serial.println(TotalDelayTime*DELAY_TIME);
}
void setup()
{
Serial.begin(SerialBaud);
Serial.println(" initalize gpio pins");
ButtonSpeedChange.begin(PIN_BUTTON);
//ButtonSpeedChange.setButtonStateFunction(myButtonStateHandler);
ButtonSpeedChange.setTapHandler(myTapHandler);
analogReadResolution(ANALOG_RES);
for(int i=0; i<LED_COUNT; i++)
{
pinMode(i, OUTPUT);
}
Serial.println("starting loop");
}
void loop()
{
LedValueOld = LedValue;
PotValue = analogRead(PIN_POT);
LedValue = map(PotValue, 0,ANALOG_MAX_VAL, 0,LED_COUNT);
if(LedValue != LedValueOld)
{
Serial.print(PotValue);
Serial.print('(');
Serial.print(LedValue);
Serial.print(')');
for(int sp=0; sp<2*LedValue; sp++) Serial.print(' ');
Serial.println('*');
}
for(int idx=0; idx<LED_COUNT; idx++)
{
digitalWrite(PIN_LEDS[idx], idx < LedValue);
}
for(int delays=0; delays<TotalDelayTime; delays++)
{
delay(DELAY_TIME);
ButtonSpeedChange.loop();
}
}