SO I'm basically trying to get the Output of "Mode [Variable modeValue]" to jump to a specific part in the program.
used this as source for the porgarm layout: Sample program
}
//Basic Program
void loop() {
EEPROM.update(storageAdress, modeValue);
modeValue = EEPROM.get(storageAdress, modeValue);
// read the value from the sensor:
Button = analogRead(buttonPin);
if (Button == HIGH)
modeValue++;
string modeValue(modeValue);
string Mode("Mode" + modeValue + "()");
Mode;
}
Also I'm trying to covert the Number of LEDs that is entered by the user to the closest number of 2x-1 with this and it wont work:
// Modes
Mode1: //-RAINBOWLOOP FOR CIRCULAR INSTALLATIONS - ADJUSTS LEDS TO MATCH CIRCULAR COLOR SHAPE
int nearest(int myArray[], int elements, bool sorted)
int idx[] = {7, 15, 31, 63, 125, 255, 511};
int distance = abs(myArray[idx] - NUMBER_LEDS);
for (int i = 1; i < elements; i++)
{
int d = abs(myArray[i] - NUMBER_LEDS);
if (d < distance)
{
idx = i;
distance = d;
}
else if (sorted) return idx;
}
return idx;
for (int i = 0; i | NUM_LEDS; idx++) {
leds[idx] = CHSV(hue[i]++, 255, 255);
}
FastLED.show();
delay(SPEED);
Another question: Am I able to fold up some of the code (like void setup() and void loop()) to get a better overview and not get confused wich part of the script I'm fiddling with...
analogRead() does not return LOW/HIGH. It returns a value from 0...1023 so it may actually work (LOW==0, HIGH==1) but buttonPin isn't defined so who knows? Only you...
It looks like you need a switch/case statement. The referenced code also uses enumeration types (that is where the 'Mode1' comes from... It is just an int)
something like this:
enum modes { Mode0, Mode1, Mode2, Mode3 };
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while(!Serial);
}
void loop() {
// put your main code here, to run repeatedly:
int currentMode = random(0,4);
switch (currentMode ) {
case Mode0:
Serial.println("0");
break;
case Mode1:
Serial.println("1");
break;
case Mode2:
Serial.println("2");
break;
case Mode3:
Serial.println("3");
break;
default:
Serial.println("something else");
break;
}
}
UPDATE:
ooof so I kinda got it to work now by just doing the correct syntax facepalm but I want to read out a value from the EEPROM which I perviosly set to 1 but it always outputs...
trying to map the output of the pin so if it is connected to the 3.3V it outputs 1 but I only know that 5V is 1023...
So I set it to 1023 being 1 for the Button Value
My Syntax is all correct but now I get the number 65281 (I guess the max number of 16bit or smt..)
Please look over it and find me the error im too dumb rn and I've been sitting in front of this for about 5 hours...
Thanks in advance
Edit:
This is at the top of my script, the setup and loop functions are at the bottom to easier edit the LED Strip modes I'm working on...
// Libraries
#include <EEPROM.h>
#include <FastLED.h>
// User variables
#define buttonPin A5 // select the input pin for the Mode Changer Button
#define storageAdress 1 // selects the onboard storage byte for settings storing
#define LED_TYPE WS2812B // enter your LED type
#define LED_PIN 4 // enter the digital pin, you connected the LED strip to
#define NUM_LEDS 127 // Number of LEDs on the strip / to use
#define FLOW_DIR ++ // either ++ or -- to reverse the effects
#define SPEED 20 // Speed of the effects. Higher value = faster effect
#define BRIGHTNESS 30 // from 0 to 255
#define COLOR_ORDER GRB // change to match your LED strip
#define CLOCK_PIN 1 // only neccesary for LEDs with clock pin
// Variables used throuout the program
CRGBArray<NUM_LEDS> leds;
uint8_t hue[NUM_LEDS];
int BOTTOM_INDEX = 0;
int TOP_INDEX = int(NUM_LEDS / 2);
int EVENODD = NUM_LEDS % 2;
unsigned int modeValue = 5; // variable to store the value coming from the Button
int Button = LOW;
int ledsX[NUM_LEDS][3]; //-ARRAY FOR COPYING WHATS IN THE LED STRIP CURRENTLY (FOR CELL-AUTOMATA, ETC)
ok, didnt think about that digitalRead outputs HIGH or LOW but how can I even remotely prevent this (this was in the digitalRead reference)
connect it to a very low set pwm pin and the button to the 3V?
Notes and Warnings
If the pin isn’t connected to anything, digitalRead() can return either HIGH or LOW (and this can change randomly).
The analog input pins can be used as digital pins, referred to as A0, A1, etc.
Are you intending to attempt to create a function name that you'll execute later?
Oh it looks like he is a few lines later:
stringThree;
That's not going to work like that. You have to understand what a compiler does. When it is done the whole thing is turned into machine language. At runtime functions and variables don't have names, they're just addresses.
You can do what you want, but it involves arrays of function pointers and it is a relatively advanced concept. Better get the basics down first.
Are you intending to attempt to create a function name that you'll execute later?
So if modeValue = 2, stringThree would be "Mode2()"? What is the purpose of that formatting?
Also, how big can modeValue get?
I am trying to go to a fuction in the script that is located above the setup and loop.
The example for this scheme is this but the script is outdated
Blackfin:
Can you post the content of that link in code tags? Kinda reluctant to click on weirdly named URLs...
Also, what's the largest number modeValue will reach? You show a numberModes compare; what is numberModes?
this is my code rn:
// Libraries
#include <EEPROM.h>
#include <FastLED.h>
// User variables
#define buttonPin A5 // select the input pin for the Mode Changer Button
#define storageAdress 1 // selects the onboard storage byte for settings storing
#define LED_TYPE WS2812B // enter your LED type
#define LED_PIN 4 // enter the digital pin, you connected the LED strip to
#define NUM_LEDS 127 // Number of LEDs on the strip / to use
#define FLOW_DIR ++ // either ++ or -- to reverse the effects
#define SPEED 20 // Speed of the effects. Higher value = faster effect
#define BRIGHTNESS 30 // from 0 to 255
#define COLOR_ORDER GRB // change to match your LED strip
#define CLOCK_PIN 1 // only neccesary for LEDs with clock pin
// Variables used throuout the program
CRGBArray<NUM_LEDS> leds;
uint8_t hue[NUM_LEDS];
int BOTTOM_INDEX = 0;
int TOP_INDEX = int(NUM_LEDS / 2);
int EVENODD = NUM_LEDS % 2;
int modeValue = 5; // variable to store the value coming from the Button
int Button = 0;
int ledsX[NUM_LEDS][3]; //-ARRAY FOR COPYING WHATS IN THE LED STRIP CURRENTLY (FOR CELL-AUTOMATA, ETC)
// Modes
#define NumberModes 3 //-Please enter the highest Mode number here
void Mode0() { //-Turned Off
}
this is the code from the page but its too long so here's the .ino in my create library:
How about using the mode number as an index into an array of function pointers to call your different mode funcs?
Here's some code based on what you started with that uses that principle. I used three modes but you could use as many as you have space for.
#include <EEPROM.h>
// Setup
const int storageAddress = 1;
const int buttonPin = A5;
// how many valid modes are there
#define NUM_MODES 3
//create prototypes of your mode function(s)
void Mode1Function( void );
void Mode2Function( void );
void Mode3Function( void );
//create a variable type
// the type is called MyFunc; it is a pointer to function that returns void and accepts void
// if you need the functions to return, say, an int, use a declaration like:
// typedef int (*MyFunc)(void);
// if you need the functions to accept data, say two integers, use this:
// typedef void (*MyFunc)(int,int);
// mix and match as required
typedef void (*MyFunc)(void);
//create a variable array of type MyFunc, in this case called Functions. Initialize
//it with the addresses of the functions you want to call
MyFunc Functions[] =
{
&Mode1Function, //when mode = 0
&Mode2Function, //when mode = 1
&Mode3Function //when mode = 2 etc
};
#define BTN_READ_INTERVAL 30U //30mS button read interval
#define FUNC_DELAY_TIME 1000U
int
modeValue,
Button,
lastButton;
void setup()
{
//for dbugging
Serial.begin(9600);
while(!Serial);
//LEDS.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
// LEDS.addLeds<LED_TYPE, LED_PIN, CLOCK_PIN COLOR_ORDER>(leds, NUM_LEDS);
//FastLED.setBrightness(BRIGHTNESS);
//for (int i = 0; i < NUM_LEDS; i++)
//{
// hue[i] = 255 / NUM_LEDS * i;
//}//for
modeValue = 0;
//keep a record of the last button press to you can detect changes in the
//button state, not just its present level
pinMode(buttonPin, INPUT_PULLUP);
lastButton = digitalRead(buttonPin);
}//setup
//Basic Program
void loop()
{
ReadButton();
}//loop
void ReadButton( void )
{
unsigned long
timeNow;
int
debounceRead;
static unsigned long
timeDelay = 0,
timeButton = millis();
//check the button once every BTN_READ_INTERVAL mS
//you can control you quickly the program responds to button presses
//(and runs the appropriate mode function) with this
timeNow = millis();
if( (timeNow - timeButton) < timeDelay )
return;
timeButton = timeNow;
timeDelay = BTN_READ_INTERVAL;
debounceRead = digitalRead(buttonPin);
delay(10);
Button = digitalRead(buttonPin);
if( debounceRead != Button )
return;
//if the button is low now but was high previously, the button has been pressed
if( Button == LOW && lastButton == HIGH )
{
modeValue++;
//very important to limit the value of modeValue to the number of
//mode functions you have
if( modeValue >= NUM_MODES )
modeValue = 0;
//index into the Functions variable you created earlier and call the
//function pointed to
Functions[modeValue]();
timeDelay = FUNC_DELAY_TIME;
EEPROM.update(storageAddress, modeValue - 256);
modeValue = EEPROM.get(storageAddress, modeValue) + 256;
}//if
lastButton = Button;
}//ReadButton
void Mode1Function( void )
{
Serial.print( "Mode 1 Function: " ); Serial.println( modeValue );
}//Mode1Function
void Mode2Function( void )
{
Serial.print( "Mode 2 Function: " ); Serial.println( modeValue );
}//Mode2Function
void Mode3Function( void )
{
Serial.print( "Mode 3 Function: " ); Serial.println( modeValue );
}//Mode3Function