Hjälp att få NeoPixlar att fadea i if-sats

C, C++, Pascal, Assembly, Raspberry, Java, Matlab, Python, BASIC, SQL, PHP, etc.
Rogga10000
Inlägg: 17
Blev medlem: 3 juni 2012, 00:14:33
Ort: Sundsvall

Hjälp att få NeoPixlar att fadea i if-sats

Inlägg av Rogga10000 »

Hej,
Håller på att skriva kod i Arduino IDE för programmering av en ATtiny 85 som skall styra två NeoPixlar i en sensor som skall ändra färg vid olika situationer

När temperaturen i rummet understiger inställt värde skall den t.ex. lysa blått.
När jag styr temperaturen (Börvärdet) via Z-Wave skall den lysa Mangenta i en halv sek var 5 sek osv.

Bifogar min kod nedan som tänder och släcker NeoPixeln digital av och på. Har nu försökt att få till en funktion att få NeoPixeln att fadea upp och ner till inställt värde. Har tittat på andra projekt och förstått att det skall vara en funktion av typ [for(int k = 0; k < 256; k++))]. Har dock inte lyckats få den att fungera i min if-sats.

Någon som har några kloka råd?

KOD:

const int BluePin = 2;
const int Blue_MangentaPin = 1;
const int MangentaPin = 0;
const int RedPin = 3;

//variabel för att avläsa ingångsstatus
int BlueState = 0;
int Blue_MangentaState = 0;
int MangentaState = 0;
int RedState = 0;

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

const int NeoPIN = 4;

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 2

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, NeoPIN, NEO_RGB + NEO_KHZ800);

int delayval = 500; // delay

void setup() {
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code

pixels.begin(); // This initializes the NeoPixel library.
pixels.show(); // initialize all pixels to "off"
pinMode(BluePin, INPUT);
pinMode(Blue_MangentaPin, INPUT);
pinMode(MangentaPin, INPUT);
pinMode(RedPin, INPUT);
}

void loop() {
BlueState = digitalRead(BluePin);
Blue_MangentaState = digitalRead(Blue_MangentaPin);
MangentaState = digitalRead(MangentaPin);
RedState = digitalRead(RedPin);

if (BlueState == HIGH && Blue_MangentaState == LOW && MangentaState == LOW && RedState == LOW) {
pixels.setPixelColor(0, pixels.Color(0,0,15)); // Blå färg.
pixels.setPixelColor(1, pixels.Color(0,0,15)); // Blå färg.

pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}

if (BlueState == LOW && Blue_MangentaState == HIGH && MangentaState == LOW && RedState == LOW) {
pixels.setPixelColor(0, pixels.Color(0,0,15)); // Blå
pixels.setPixelColor(1, pixels.Color(0,0,15)); // Blå

pixels.show(); // This sends the updated pixel color to the hardware.
delay(5000);
pixels.setPixelColor(0, pixels.Color(15,0,15)); // Mangenta
pixels.setPixelColor(1, pixels.Color(15,0,15)); // Mangenta

pixels.show(); // This sends the updated pixel color to the hardware.
delay(500);
}

if (BlueState == LOW && Blue_MangentaState == LOW && MangentaState == HIGH && RedState == LOW) {
pixels.setPixelColor(0, pixels.Color(15,0,15)); // Mangenta
pixels.setPixelColor(1, pixels.Color(0,0,0));

pixels.show(); // This sends the updated pixel color to the hardware.
delay(3000);
pixels.setPixelColor(0, pixels.Color(0,0,0));
pixels.setPixelColor(1, pixels.Color(15,0,15)); // Mangenta

pixels.show(); // This sends the updated pixel color to the hardware.
delay(3000);
}

if (BlueState == LOW && Blue_MangentaState == LOW && MangentaState == LOW && RedState == HIGH) {
pixels.setPixelColor(0, pixels.Color(15,0,0)); // Röd
pixels.setPixelColor(1, pixels.Color(0,0,0)); //

pixels.show(); // This sends the updated pixel color to the hardware.
delay(50);
pixels.setPixelColor(0, pixels.Color(0,0,0)); // .
pixels.setPixelColor(1, pixels.Color(15,0,0)); // Röd

pixels.show(); // This sends the updated pixel color to the hardware.
delay(50);
}

else {
pixels.setPixelColor(0, pixels.Color(0,0,0));
pixels.setPixelColor(1, pixels.Color(0,0,0));

pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
Senast redigerad av Rogga10000 29 december 2016, 18:06:00, redigerad totalt 1 gång.
Användarvisningsbild
AndLi
Inlägg: 17101
Blev medlem: 11 februari 2004, 18:17:59
Ort: Knivsta
Kontakt:

Re: Hjälp att få NeoPixal att fada i if-sats

Inlägg av AndLi »

code tagarna gör din kod mer lättläst...

En fultoning måste väl ske med följande kod?

Kod: Markera allt

for(i=15;i<15;i--) //eller hur man nu skriver for loopar i vilket språk det nu är skrivet i...
pixels.setPixelColor(1, pixels.Color(0,0,i)); // Blå
pixels.show();
delay(100);
Rogga10000
Inlägg: 17
Blev medlem: 3 juni 2012, 00:14:33
Ort: Sundsvall

Re: Hjälp att få NeoPixal att fada i if-sats

Inlägg av Rogga10000 »

Hej och tack för snabbt svar.
Jag klistrade in din kod så här:

Kod: Markera allt

if (BlueState == LOW && Blue_MangentaState == HIGH && MangentaState == LOW && RedState == LOW) {
  for(i=15 ; i < 15 ; i ++) //eller hur man nu skriver for loopar i vilket språk det nu är skrivet i...
  pixels.setPixelColor(1, pixels.Color(0,0,i)); // Blå
  pixels.show();
  delay(100);
  }
och fick detta svar vid kompilering

'i' was not declared in this scope

Koden är skriven i C

/Roger
Användarvisningsbild
AndLi
Inlägg: 17101
Blev medlem: 11 februari 2004, 18:17:59
Ort: Knivsta
Kontakt:

Re: Hjälp att få NeoPixlar att fadea i if-sats

Inlägg av AndLi »

släng in en int i; uppe vid de andra raderna med int först så funkar det...

Nä C är det inte då C inte stödjer klasser....
Men Arduino använder väl någon egenmodifierad version av C++..
Rogga10000
Inlägg: 17
Blev medlem: 3 juni 2012, 00:14:33
Ort: Sundsvall

Re: Hjälp att få NeoPixlar att fadea i if-sats

Inlägg av Rogga10000 »

Tack så mycket för hjälpen. Fick inga felmeddelanden och lyckades ladda in koden i en ATtiny45.

Tyvärr fungerade det inget vidare. Blev alla möjliga blinkande färger.

Kan det vara något med kretsens minne. Kan testa att ladda den i en ATtiny 85 i morgon eller köra den direkt från Arduino Uno. Undrar även om det är bättre att använda if else i stället för if?

Så här ser min kod ut nu:

Kod: Markera allt

const int BluePin = 2;
const int Blue_MangentaPin = 1;
const int MangentaPin = 0;
const int RedPin = 3;

//variabel för att avläsa ingångsstatus
int BlueState = 0; 
int Blue_MangentaState = 0;
int MangentaState = 0;
int RedState = 0;
int i;
int j;
int k;

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

const int NeoPIN = 4;

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      2

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, NeoPIN, NEO_RGB + NEO_KHZ800);

int delayval = 10; // delay 

void setup() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  // End of trinket special code

pixels.begin(); // This initializes the NeoPixel library.
pixels.show(); // initialize all pixels to "off"
pinMode(BluePin, INPUT);
pinMode(Blue_MangentaPin, INPUT);
pinMode(MangentaPin, INPUT);
pinMode(RedPin, INPUT);
}

void loop() {
BlueState = digitalRead(BluePin);
Blue_MangentaState = digitalRead(Blue_MangentaPin);
MangentaState = digitalRead(MangentaPin);
RedState = digitalRead(RedPin);


if (BlueState == HIGH && Blue_MangentaState == LOW && MangentaState == LOW && RedState == LOW) {
pixels.setPixelColor(0, pixels.Color(0,0,15)); // Blå färg.
pixels.setPixelColor(1, pixels.Color(0,0,15)); // Blå färg.

pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
  
pixels.setPixelColor(0, pixels.Color(0,0,15)); // Blå färg.
pixels.setPixelColor(1, pixels.Color(0,0,15)); // Blå färg.

pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).  
}

if (BlueState == LOW && Blue_MangentaState == HIGH && MangentaState == LOW && RedState == LOW) {
for(i = 0; i < 15; i++) //Fade in
pixels.setPixelColor(0, pixels.Color(0,0,i)); // Blå
pixels.setPixelColor(1, pixels.Color(0,0,i)); // Blå
pixels.show();
delay(4000);
for(i = 15; i >= 15; i--) // Fade out
pixels.setPixelColor(0, pixels.Color(0,0,i)); // Blå
pixels.setPixelColor(1, pixels.Color(0,0,i)); // Blå
pixels.show();
delay(delayval);
  
for(j = 0; j < 15; j++) //Fade in
pixels.setPixelColor(0, pixels.Color(j,0,j)); // Mangenta
pixels.setPixelColor(1, pixels.Color(j,0,j)); // Mangenta
pixels.show();
delay(500);
for(j = 15; j >= 15; j--) // Fade out
pixels.setPixelColor(0, pixels.Color(j,0,j)); // Mangenta
pixels.setPixelColor(1, pixels.Color(j,0,j)); // Mangenta
pixels.show();
delay(delayval);  
 }

if (BlueState == LOW && Blue_MangentaState == LOW && MangentaState == HIGH && RedState == LOW) {
for(k = 0; k < 15; k++) //Fade in
pixels.setPixelColor(0, pixels.Color(k,0,k)); // Mangenta
pixels.setPixelColor(1, pixels.Color(k,0,k)); // Mangenta
pixels.show();
delay(500);
for(k = 15; k >= 15; k--) // Fade out
pixels.setPixelColor(0, pixels.Color(k,0,k)); // Mangenta
pixels.setPixelColor(1, pixels.Color(k,0,k)); // Mangenta
pixels.show();
delay(4000);  
  }
  
if (BlueState == LOW && Blue_MangentaState == LOW && MangentaState == LOW && RedState == HIGH) {
pixels.setPixelColor(0, pixels.Color(15,0,0)); // Röd
pixels.setPixelColor(1, pixels.Color(0,0,0)); // 

pixels.show(); // This sends the updated pixel color to the hardware.
delay(50);
pixels.setPixelColor(0, pixels.Color(0,0,0)); // .
pixels.setPixelColor(1, pixels.Color(15,0,0)); // Röd

pixels.show(); // This sends the updated pixel color to the hardware.
delay(50); 
 }

else {
pixels.setPixelColor(0, pixels.Color(0,0,0));
pixels.setPixelColor(1, pixels.Color(0,0,0));

pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
  }
}
Användarvisningsbild
AndLi
Inlägg: 17101
Blev medlem: 11 februari 2004, 18:17:59
Ort: Knivsta
Kontakt:

Re: Hjälp att få NeoPixlar att fadea i if-sats

Inlägg av AndLi »

Vi har missat lite måsvingar.... {}

Sätt en på raden efter for och en efter delay så bör det funka bättre...
Användarvisningsbild
ffredrik
Inlägg: 341
Blev medlem: 20 oktober 2009, 17:52:18
Ort: Göinge

Re: Hjälp att få NeoPixlar att fadea i if-sats

Inlägg av ffredrik »

Ändra
for(j = 15; j >= 15; j--) // Fade out

till
for(j = 15; j >= 0;j--) // Fade out

på alla tre ställena (i, j, k).
Skriv svar