106Views5Replies
How can I change an "if" in an arduino code ?
So I have this code from James Hutton and what I want to do is to change the "rainbowCycle" to something else, like "NeoPixel_Cylon_Scrolling_Eye". Is it possible ? Thank you very much !
So I have this code from James Hutton and what I want to do is to change the "rainbowCycle" to something else, like "NeoPixel_Cylon_Scrolling_Eye". Is it possible ? Thank you very much !
Comments
Best Answer 4 years ago
You just copy code you do not know what it is doing.
When I get into such a situation I will make a flow chart out of each statement like that do while loop for the whole code until I see what function it performs.
Answer 4 years ago
Hi, thank you.
I did manage to work it out, now when there is no music, the cylon effect kicks in, the problem i have now, is that it wont go back to the original code, when it gets music.
Can you please have a look?
Thank you
The modified code:
================
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define lightCount 286
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream
// NEO_HZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(lightCount, PIN, NEO_GRB + NEO_KHZ800);
//fadeThickness = 0; // CHANGE THICKNESS (0-10)
int vol = 0;
float total = 0;
int fadeCol = 0;
int val[25];
int volLast = 0;
int fadeAmt = 0;
int counter = 0;
int wait_T=10; //This is the delay between moving back and forth and per pixel
int PixelCount=286; //Set this to the AMOUNT of Led's/Pixels you have or want to use on your strip And It can be used to tell where to Stop then return the eye at in the strip
int Pixel_Start_End=0; //Set this to where you want it to Start/End at
boolean UsingBar = false; //Set this to true If you are using the 8x1 Neopixel Bar Or you want to only use 3 leds for the scanner.
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
//Serial.begin(9600);
}
void loop() {
fadeCol = 0;
total = 0;
for (int i = 0; i < 286; i++){
counter = 0;
do{
vol = analogRead(A0);
counter = counter + 1;
if (counter > 500){
//Example: CylonEyeUp(Center_Dot_Color, Second_Dot_color, Third_Dot_color, wait_T, PixelCount, Pixel_Start_End);
CylonEyeUp(strip.Color(255,0,0), strip.Color(25,0,0), strip.Color(10,0,0), wait_T, PixelCount, Pixel_Start_End);
delay(wait_T);
//Example: CylonEyeDown(Center_Dot_Color, Second_Dot_color, Third_Dot_color, wait_T, PixelCount, Pixel_Start_End);
CylonEyeDown(strip.Color(255,0,0), strip.Color(25,0,0), strip.Color(10,0,0), wait_T, PixelCount, Pixel_Start_End);
delay(wait_T);
}
}while (vol == 0);
total = total + vol;
}
vol = total / 286;
// Serial.println(vol);
vol = map(vol,20,40,0,20);
if (volLast > vol) {
vol = volLast - 7;
}
volLast = vol;
fadeAmt = 0;
// Serial.print(vol);
for (int i = 0; i<286;i++){
if (i < vol){
strip.setPixelColor((i+0), strip.Color(0,0,255));
strip.setPixelColor((0-i), strip.Color(0,0,255));
}
else if (i < (vol + 15)) {
strip.setPixelColor((i+0), strip.Color(255,255,255));
strip.setPixelColor((0-i), strip.Color(255,255,255));
}
else
{
strip.setPixelColor((i+0), strip.Color(0,0,0));
strip.setPixelColor((0-i), strip.Color(0,0,0));
}
}
strip.show();
}
void CylonEyeUp(uint32_t Co, uint32_t Ct, uint32_t Ctt, uint8_t Delay, int TotalPixels, int pStart) {
for(int i=pStart; i<TotalPixels; i++) {
if(!UsingBar) { strip.setPixelColor(i+2, Ctt); } //Third Dot Color
strip.setPixelColor(i+1, Ct); //Second Dot Color
strip.setPixelColor(i, Co); //Center Dot Color
strip.setPixelColor(i-1, Ct); //Second Dot Color
if(!UsingBar) { strip.setPixelColor(i-2, Ctt); } //Third Dot Color
if(!UsingBar) {
strip.setPixelColor(i-3, strip.Color(0,0,0)); //Clears the dots after the 3rd color
} else {
strip.setPixelColor(i-2, strip.Color(0,0,0)); //Clears the dots after the 2rd color
}
strip.show();
//Serial.println(i); //Used For pixel Count Debugging
delay(Delay);
}
}
void CylonEyeDown(uint32_t Co, uint32_t Ct, uint32_t Ctt, uint8_t Delay, int TotalPixels, int pEnd) {
for(int i=TotalPixels-1; i>pEnd; i--) {
if(!UsingBar) { strip.setPixelColor(i-2, Ctt); } //Third Dot Color
strip.setPixelColor(i-1, Ct); //Second Dot Color
strip.setPixelColor(i, Co); //Center Dot Color
strip.setPixelColor(i+1, Ct); //Second Dot Color
if(!UsingBar) { strip.setPixelColor(i+2, Ctt); } //Third Dot Color
if(!UsingBar) {
strip.setPixelColor(i+3, strip.Color(0,0,0)); //Clears the dots after the 3rd color
} else {
strip.setPixelColor(i+2, strip.Color(0,0,0)); //Clears the dots after the 2rd color
}
strip.show();
//Serial.println(i); //Used For pixel Count Debugging
delay(Delay);
}
}
4 years ago
So first of all thank you for the explanation, i do appreciate your long answer!
But i don't really have time to learn the this coding... so what I need is someone who can make this happen, or can give me examples.
Thank you!
Answer 4 years ago
Ah...yes, I see. You don't have time and inclination to learn and you want someone else to do the work for you. well, ok. good luck.
4 years ago
Yes. Its possible. What happens when you try it ?
Answer 4 years ago
So, I don't even know where to insert the code...
This is the code:
==============
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define lightCount 286
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream
// NEO_HZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(lightCount, PIN, NEO_GRB + NEO_KHZ800);
//fadeThickness = 0; // CHANGE THICKNESS (0-10)
int vol = 0;
float total = 0;
int fadeCol = 0;
int val[25];
int volLast = 0;
int fadeAmt = 0;
int counter = 0;
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
//Serial.begin(9600);
}
void loop() {
fadeCol = 0;
total = 0;
for (int i = 0; i < 286; i++){
counter = 0;
do{
vol = analogRead(A0);
counter = counter + 1;
if (counter > 500){
rainbowCycle(10);
}
}while (vol == 0);
total = total + vol;
}
vol = total / 286;
// Serial.println(vol);
vol = map(vol,20,40,0,20);
if (volLast > vol) {
vol = volLast - 7;
}
volLast = vol;
fadeAmt = 0;
// Serial.print(vol);
for (int i = 0; i<286;i++){
if (i < vol){
strip.setPixelColor((i+143), strip.Color(0,255,0));
strip.setPixelColor((143-i), strip.Color(0,255,0));
}
else if (i < (vol + 20)) {
strip.setPixelColor((i+143), strip.Color(255,0,0));
strip.setPixelColor((143-i), strip.Color(255,0,0));
}
else
{
strip.setPixelColor((i+143), strip.Color(0,0,0));
strip.setPixelColor((143-i), strip.Color(0,0,0));
}
}
strip.show();
}
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
vol = analogRead(A0);
if (vol> 10) {
return;
}
}
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}
Where do I insert the other code ?:
===========================
//NeoPixel LED Digital Strip Cylon Eye v1.10 Created by EternalCore
#include <Adafruit_NeoPixel.h>
//Settings:
#define PIN 6 //The Pin out your Neopixel DIN strip/stick is connected to (Default is 6)
#define TPIXEL 284 //The total amount of pixel's/led's in your connected strip/stick (Default is 60)
int wait_T=40; //This is the delay between moving back and forth and per pixel
int PixelCount=284; //Set this to the AMOUNT of Led's/Pixels you have or want to use on your strip And It can be used to tell where to Stop then return the eye at in the strip
int Pixel_Start_End=0; //Set this to where you want it to Start/End at
boolean UsingBar = false; //Set this to true If you are using the 8x1 Neopixel Bar Or you want to only use 3 leds for the scanner.
Adafruit_NeoPixel strip = Adafruit_NeoPixel(TPIXEL, PIN, NEO_GRB + NEO_KHZ800); //Standered Strip function
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
//Serial.begin(9600); //Used For pixel Count Debugging
}
void loop() {
//Example: CylonEyeUp(Center_Dot_Color, Second_Dot_color, Third_Dot_color, wait_T, PixelCount, Pixel_Start_End);
CylonEyeUp(strip.Color(175,0,0), strip.Color(25,0,0), strip.Color(10,0,0), wait_T, PixelCount, Pixel_Start_End);
delay(wait_T);
//Example: CylonEyeDown(Center_Dot_Color, Second_Dot_color, Third_Dot_color, wait_T, PixelCount, Pixel_Start_End);
CylonEyeDown(strip.Color(175,0,0), strip.Color(25,0,0), strip.Color(10,0,0), wait_T, PixelCount, Pixel_Start_End);
delay(wait_T);
}
void CylonEyeUp(uint32_t Co, uint32_t Ct, uint32_t Ctt, uint8_t Delay, int TotalPixels, int pStart) {
for(int i=pStart; i<TotalPixels; i++) {
if(!UsingBar) { strip.setPixelColor(i+2, Ctt); } //Third Dot Color
strip.setPixelColor(i+1, Ct); //Second Dot Color
strip.setPixelColor(i, Co); //Center Dot Color
strip.setPixelColor(i-1, Ct); //Second Dot Color
if(!UsingBar) { strip.setPixelColor(i-2, Ctt); } //Third Dot Color
if(!UsingBar) {
strip.setPixelColor(i-3, strip.Color(0,0,0)); //Clears the dots after the 3rd color
} else {
strip.setPixelColor(i-2, strip.Color(0,0,0)); //Clears the dots after the 2rd color
}
strip.show();
//Serial.println(i); //Used For pixel Count Debugging
delay(Delay);
}
}
void CylonEyeDown(uint32_t Co, uint32_t Ct, uint32_t Ctt, uint8_t Delay, int TotalPixels, int pEnd) {
for(int i=TotalPixels-1; i>pEnd; i--) {
if(!UsingBar) { strip.setPixelColor(i-2, Ctt); } //Third Dot Color
strip.setPixelColor(i-1, Ct); //Second Dot Color
strip.setPixelColor(i, Co); //Center Dot Color
strip.setPixelColor(i+1, Ct); //Second Dot Color
if(!UsingBar) { strip.setPixelColor(i+2, Ctt); } //Third Dot Color
if(!UsingBar) {
strip.setPixelColor(i+3, strip.Color(0,0,0)); //Clears the dots after the 3rd color
} else {
strip.setPixelColor(i+2, strip.Color(0,0,0)); //Clears the dots after the 2rd color
}
strip.show();
//Serial.println(i); //Used For pixel Count Debugging
delay(Delay);
}
}
Thank you!