Hello. Hopefully what I'm trying to accomplish is clear. Basically, I am trying to modify this code to work with a Pro Micro for PWM control. The pins on the Pro Micro (ATMega32u4 Chipset) are not all PWM. The code by default, uses a ++ to count the pins linearly, but I want to be able to assign pins manually.
So originally its set as, uint8_t curLightsPins = 2, so it sets a starting value, in the case 2, and would count up each pin using kLightsPin(curLightPin++), to assign each button to pins 2, pin 3, etc.
I modified the code so instead of a value that counts +1, it uses a static array of assigned values corresponding with PWM available pins, with an array uint8_t curLightPin [] {6,3,9,5};
Below is the relevant segment of code I am trying to modify, and the link for the git source code is available here: https://github.com/teejusb/fsr/blob/master/fsr.ino
// EXPERIMENTAL. Used to turn on the lights feature. Note, this might conflict
// some existing sensor pins so if you see some weird behavior it might be
// because of this. Uncomment the following line to enable the feature.
#define ENABLE_LIGHTS
// We don't want to use digital pins 0 and 1 as they're needed for Serial
// communication so we start curLightPin from 2.
// Automatically incremented when creating a new SensorState.
**#if defined(ENABLE_LIGHTS)**
**uint8_t curLightPin [] {6,3,9,5};**
#endif
/*===========================================================================*/
// The class that actually evaluates a sensor and actually triggers the button
// press or release event. If there are multiple sensors added to a
// SensorState, they will all be evaluated first before triggering the event.
class SensorState {
public:
SensorState()
: num_sensors_(0),
#if defined(ENABLE_LIGHTS)
**kLightsPin(curLightPin [0]),**
** #endif**
** buttonNum(curButtonNum++)** {
for (size_t i = 0; i < kMaxSharedSensors; ++i) {
sensor_ids_[i] = 0;
individual_states_[i] = SensorState::OFF;
}
#if defined(ENABLE_LIGHTS)
pinMode(kLightsPin, OUTPUT);
#endif
}
void Init() {
if (initialized_) {
return;
}
buttonNum = curButtonNum++;
initialized_ = true;
}
// Adds a new sensor to share this state with. If we try adding a sensor that
// we don't have space for, it's essentially dropped.
void AddSensor(uint8_t sensor_id) {
if (num_sensors_ < kMaxSharedSensors) {
sensor_ids_[num_sensors_++] = sensor_id;
}
}
// Evaluates a single sensor as part of the shared state.
void EvaluateSensor(uint8_t sensor_id,
int16_t cur_value,
int16_t user_threshold) {
if (!initialized_) {
return;
}
size_t sensor_index = GetIndexForSensor(sensor_id);
// The sensor we're evaluating is not part of this shared state.
// This should not happen.
if (sensor_index == SIZE_MAX) {
return;
}
// If we're above the threshold, turn the individual sensor on.
if (cur_value >= user_threshold + kPaddingWidth) {
individual_states_[sensor_index] = SensorState::ON;
}
// If we're below the threshold, turn the individual sensor off.
if (cur_value < user_threshold - kPaddingWidth) {
individual_states_[sensor_index] = SensorState::OFF;
}
// If we evaluated all the sensors this state applies to, only then
// should we determine if we want to send a press/release event.
bool all_evaluated = (sensor_index == num_sensors_ - 1);
if (all_evaluated) {
switch (combined_state_) {
case SensorState::OFF:
{
// If ANY of the sensors triggered, then we trigger a button press.
bool turn_on = false;
for (size_t i = 0; i < num_sensors_; ++i) {
if (individual_states_[i] == SensorState::ON) {
turn_on = true;
break;
}
}
if (turn_on) {
ButtonPress(buttonNum);
combined_state_ = SensorState::ON;
#if defined(ENABLE_LIGHTS)
analogWrite(kLightsPin, 255);
#endif
}
}
break;
case SensorState::ON:
{
// ALL of the sensors must be off to trigger a release.
// i.e. If any of them are ON we do not release.
bool turn_off = true;
for (size_t i = 0; i < num_sensors_; ++i) {
if (individual_states_[i] == SensorState::ON) {
turn_off = false;
}
}
if (turn_off) {
**ButtonRelease(buttonNum);**
** combined_state_ = SensorState::OFF;**
** #if defined(ENABLE_LIGHTS)**
** analogWrite(kLightsPin, 35); //This was originally digitalWrite, but we need analogWrite for brightness control for the LEDS**
#endif
}
}
break;
}
}
}
// Given a sensor_id, returns the index in the sensor_ids_ array.
// Returns SIZE_MAX if not found.
size_t GetIndexForSensor(uint8_t sensor_id) {
for (size_t i = 0; i < num_sensors_; ++i) {
if (sensor_ids_[i] == sensor_id) {
return i;
}
}
return SIZE_MAX;
}
private:
// Ensures that Init() has been called at exactly once on this SensorState.
bool initialized_;
// The collection of sensors shared with this state.
uint8_t sensor_ids_[kMaxSharedSensors];
// The number of sensors this state combines with.
size_t num_sensors_;
// Used to determine the state of each individual sensor, as well as
// the aggregated state.
enum State { OFF, ON };
// The evaluated state for each individual sensor.
State individual_states_[kMaxSharedSensors];
// The aggregated state.
State combined_state_ = SensorState::OFF;
// One-tailed width size to create a window around user_threshold to
// mitigate fluctuations by noise.
// TODO(teejusb): Make this a user controllable variable.
const int16_t kPaddingWidth = 1;
// The light pin this state corresponds to.
#if defined(ENABLE_LIGHTS)
const uint8_t kLightsPin;
#endif
// The button number this state corresponds to.
// Set once in Init().
uint8_t buttonNum;
};
I did search tutorials and did my best to Google search, but I cannot find a specific guide for this type of array implementation. I apologize if this is a very beginner question or comes off as "please write my code for me", I'm trying my best. Thank you for any possible assistance.
TL;DR I wan kLightsPin to reference the array values instead of just counting +1 for each assigned pin.
Edit: Ignore the asterisks in the code, I tried bolding it but I didn't realize bolding on the forum post doesn't work when it's also put in as code.