Exit function

Arduino specifieke Software
Berichten: 10
Geregistreerd: 10 Aug 2020, 20:29
Woonplaats: 's-Hertogenbosch (Hintham)

Exit function

Berichtdoor Nicoooootje » 10 Aug 2020, 20:38

Code: Alles selecteren
// Edit by Serge Niko June 2015

#include <Adafruit_NeoPixel.h>

#define PIN D7

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)

Adafruit_NeoPixel strip = Adafruit_NeoPixel(299, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

// Set up Variables
unsigned long timeOut = 60000; // timestamp to remember when the PIR was triggered.
int downUp = 0;              // variable to rememer the direction of travel up or down the stairs
int alarmPinTop = D5;        // PIR at the top of the stairs
int alarmPinBottom = D6;     // PIR at the bottom of the stairs
int alarmValueTop = LOW;    // Variable to hold the PIR status
int alarmValueBottom = LOW; // Variable to hold the PIR status
int ledPin = 13;           // LED on the arduino board flashes when PIR activated
int LDRSensor = A0;        // Light dependant resistor
int LDRValue = 0;          // Variable to hold the LDR value
int colourArray[350];      // An array to hold RGB values
int change = 1;            // used in 'breathing' the LED's
int breathe = 0;           // used in 'breathing' the LED's


void setup() {
  strip.begin();
  strip.setBrightness(40); //adjust brightness here
  strip.show(); // Initialize all pixels to 'off'
  Serial.begin (9600);  // only requred for debugging
  pinMode(ledPin, OUTPUT);  // initilise the onboard pin 13 LED as an indicator
  pinMode(alarmPinTop, INPUT_PULLUP);     // for PIR at top of stairs initialise the input pin and use the internal restistor
  pinMode(alarmPinBottom, INPUT_PULLUP);  // for PIR at bottom of stairs initialise the input pin and use the internal restistor
  delay (2000); // it takes the sensor 2 seconds to scan the area around it before it can
  //detect infrared presence.

}

void loop() {
  if (alarmValueTop == LOW && alarmValueBottom == LOW && downUp == 0) {
    knightRider(1, 35, 2, 0xFF1000); // Cycles, Speed, Width, RGB Color (original orange-red)
    //delay(1000);
  }
  alarmValueTop = digitalRead(alarmPinTop);    // Constantly poll the PIR at the top of the stairs
  //Serial.println(alarmPinTop);
  alarmValueBottom = digitalRead(alarmPinBottom);  // Constantly poll the PIR at the bottom of the stairs
  //Serial.println(alarmPinBottom);

  if (alarmValueTop == HIGH && downUp != 2)  {      // the 2nd term allows timeOut to be contantly reset if one lingers at the top of the stairs before decending but will not allow the bottom PIR to reset timeOut as you decend past it.
    timeOut = millis(); // Timestamp when the PIR is triggered.  The LED cycle wil then start.
    downUp = 1;
    //clearStrip();
    topdown();         // lights up the strip from top down
  }

  if (alarmValueBottom == HIGH && downUp != 1)  {    // the 2nd term allows timeOut to be contantly reset if one lingers at the bottom of the stairs before decending but will not allow the top PIR to reset timeOut as you decend past it.
    timeOut = millis();  // Timestamp when the PIR is triggered.  The LED cycle wil then start.
    downUp = 2;
    //clearStrip();
    bottomup();         // lights up the strip from bottom up
  }

  if (timeOut + 10000 < millis() && timeOut + 15000 < millis()) { //switch off LED's in the direction of travel.
    if (downUp == 1) {
      colourWipeDown(strip.Color(0, 0, 0), 100); // Off
    }
    if (downUp == 2)  {
      colourWipeUp(strip.Color(0, 0, 0), 100);   // Off
    }
    downUp = 0;

  }



}

void topdown() {
  Serial.println ("detected top");                  // Helpful debug message
  colourWipeDown(strip.Color(255, 255, 250), 25 );  // Warm White
  //for(int i=0; i<3; i++) {                        // Helpful debug indication flashes led on Arduino board twice
  //digitalWrite(ledPin,HIGH);
  //delay(200);
  //digitalWrite(ledPin,LOW);
  //delay(200);
  //}
}
void knightRider(uint16_t cycles, uint16_t speed, uint8_t width, uint32_t color) {
  uint32_t old_val[23]; // up to 256 lights!
  // Larson time baby!

  for (int i = 0; i < cycles; i++) {
    for (int count = 1; count < 23; count++) {
      strip.setPixelColor(count, color);
      old_val[count] = color;
      for (int x = count; x > 0; x--) {
        old_val[x - 1] = dimColor(old_val[x - 1], width);
        strip.setPixelColor(x - 1, old_val[x - 1]);
      }
      strip.show();
      delay(speed);
    }
    for (int count = 23 - 1; count >= 0; count--) {
      strip.setPixelColor(count, color);
      old_val[count] = color;
      for (int x = count; x <= 23 ; x++) {

        old_val[x - 1] = dimColor(old_val[x - 1], width);
        strip.setPixelColor(x + 1, old_val[x + 1]);
      }
      strip.show();
      delay(speed);

    }
  }
}


void bottomup() {
  Serial.println ("detected bottom");            // Helpful debug message
  colourWipeUp(strip.Color(255, 255, 250), 25);  // Warm White
  //for(int i=0; i<3; i++) {                     // Helpful debug indication flashes led on Arduino board twice
  //digitalWrite(ledPin,HIGH);
  //delay(200);
  //digitalWrite(ledPin,LOW);
  //delay(200);
  //}
}

// Fade light each step strip
void colourWipeDown(uint32_t c, uint16_t wait) {

  for (uint16_t j = 0; j < 13; j++) {
    int start = strip.numPixels() / 13 * j;
    Serial.println(j);

    for (uint16_t i = start; i < start + 23; i++) {
      strip.setPixelColor(i, c);
    }
    strip.show();
    delay(wait);
  }

}


void clearStrip() {
  for (int l = 0; l < strip.numPixels(); l++) {
    strip.setPixelColor(l, (0, 0, 0));
  }

}
// Fade light each step strip
void colourWipeUp(uint32_t c, uint16_t wait) {
  for (uint16_t j = 13; j > 0; j--) {
    int start = strip.numPixels() / 13 * j;
    Serial.println(j);
    //start = start-1;
    for (uint16_t i = start; i > start - 23; i--) {
      strip.setPixelColor(i - 1, c);
    }
    strip.show();
    delay(wait);
  }
}



void clearStripKnight() {
  for ( int i = 0; i < 23; i++) {
    strip.setPixelColor(i, 0x000000); strip.show();
  }
}

uint32_t dimColor(uint32_t color, uint8_t width) {
  return (((color & 0xFF0000) / width) & 0xFF0000) + (((color & 0x00FF00) / width) & 0x00FF00) + (((color & 0x0000FF) / width) & 0x0000FF);



}



Ben nog niet zo thuis in het programmeren maar heb met kopie en plak werk en zelf wat aanpassen de hierboven staande code geschreven

Wil verlichting in mijn trap maken en hij zou het volgende moeten doen
Heb op de eerste trede de knight rider draaien en deze moet onderbroken worden als de sensor beneden of boven getriggerd word.

Op dit moment moet de sensor een langere tijd getriggerd worden voordat de knight rider stopt. Wil dus gelijk uit de knight rider functie als de onderste of de bovenste sensor getriggerd word.

Hoop dat iemand mij hier mij mee kan helpen

Advertisement

Berichten: 4067
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: Exit function

Berichtdoor shooter » 13 Aug 2020, 20:06

je zult helaas wat werk hebben om al die delays eruit te halen en te vervangen door werken met een timer (millis()).
als je dat gedaan hebt dan kun je dus in de hoofdloop zetten als sensor dan andere functie.
op die manier blijft de loop heel erg snel en kun je de status snel wijzigen.
paul deelen
shooter@home.nl

Berichten: 10
Geregistreerd: 10 Aug 2020, 20:29
Woonplaats: 's-Hertogenbosch (Hintham)

Re: Exit function

Berichtdoor Nicoooootje » 14 Aug 2020, 15:02

Dankje wel voor je reactie

Veel werk vind ik niet zo erg. Alleen weet nu nog niet zo goed hoe dit te doen.

Berichten: 4067
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: Exit function

Berichtdoor shooter » 14 Aug 2020, 20:41

begin eens simpel met het ledje 13
pak er blink without delay bij is een voorbeeld sketch
dan zul je zien dat er gebruik gemaakt wordt van millis()
dat is een teller die elke milliseconde 1 bijtelt.
als je dan de huidige tijd bekijkt met millis() en er een oude waarde vanaf trekt dan hou je dus een getal over, als dat groter is dan de gewenste wachttijd is deze voorbij en moet je naar een functie springen om iets te doen bijvorbeeld uit=!uit.
zo kun je dus elke milliseconde iets laten gebeuren. (dat gaat te snel voor de ogen)
die millis teller gaat altijd door
paul deelen
shooter@home.nl

Berichten: 4067
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: Exit function

Berichtdoor shooter » 16 Aug 2020, 09:01

laat het topic maar vollopen hoor, dat is juist de opzet van dit soort forums.
in de loop zet je die timers neer mogen er uiteraard ook meerdere zijn.
als een timer gedaan is dan spring je even naar een functie (heet bij jou void, maar dat is een ander onderwerp). daar kijk je welke situatie je hebt dus bijvoorbeeld status 60 de volgende status is dan bijv 70 dan zet je dus je lampjes aan en doe je show. en dan ben je hiermee klaar en springt je programma terug naar de main loopom te kijken of er weer een timer of zo gedaan is.
hierdoor zit je maar heel kort in een functie.
zoals je al aangaf kun je ook testen of een ingang aan is en dan spring je naar een andere functie met een andere status.

het verhaal van void is eigenlijk ook simpel, als je een functie gebruikt kun je daar ook getallen mee terug sturen naar je loop (of een ander subfunctie) en dat doe je dus met bijv void vervangen door een int of zo.

een for loop werkt nu dus niet goed meer want je wilt telkens een andere led aan gaan, dus in je functie komt ipv een for loop een teller te staan welke led aan is.
probeer it maar eens met een simpel programma in de loop komt een timer van 1500 milliseconden.
als de tijd om is ga je dus naar een functie waar je een teller 1 ophoogt bijv een serial.print erachter van die teller en klaar. dan krijg je dus een langzame opteller terwijl je ook nog een led13 kunt laten knipperen met 200 millis bijvoorbeeld
paul deelen
shooter@home.nl

Berichten: 10
Geregistreerd: 10 Aug 2020, 20:29
Woonplaats: 's-Hertogenbosch (Hintham)

Re: Exit function

Berichtdoor Nicoooootje » 17 Aug 2020, 21:22

Het is me gelukt door break; te gebruiken

Code: Alles selecteren
// Edit by Serge Niko June 2015

#include <Adafruit_NeoPixel.h>

#define PIN D7

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)

Adafruit_NeoPixel strip = Adafruit_NeoPixel(299, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

// Set up Variables
unsigned long timeOut = 60000; // timestamp to remember when the PIR was triggered.
int downUp = 0;              // variable to rememer the direction of travel up or down the stairs
int alarmPinTop = D6;        // PIR at the top of the stairs
int alarmPinBottom = D5;     // PIR at the bottom of the stairs
int alarmValueTop = LOW;    // Variable to hold the PIR status
int alarmValueBottom = LOW; // Variable to hold the PIR status
int ledPin = 13;           // LED on the arduino board flashes when PIR activated
int LDRSensor = A0;        // Light dependant resistor
int LDRValue = 0;          // Variable to hold the LDR value
int colourArray[350];      // An array to hold RGB values
int change = 1;            // used in 'breathing' the LED's
int breathe = 0;           // used in 'breathing' the LED's


void setup() {
  strip.begin();
  strip.setBrightness(40); //adjust brightness here
  strip.show(); // Initialize all pixels to 'off'
  Serial.begin (9600);  // only requred for debugging
  pinMode(ledPin, OUTPUT);  // initilise the onboard pin 13 LED as an indicator
  pinMode(alarmPinTop, INPUT_PULLUP);     // for PIR at top of stairs initialise the input pin and use the internal restistor
  pinMode(alarmPinBottom, INPUT_PULLUP);  // for PIR at bottom of stairs initialise the input pin and use the internal restistor
  delay (2000); // it takes the sensor 2 seconds to scan the area around it before it can
  //detect infrared presence.

}

void loop() {

  if (alarmValueTop == LOW && alarmValueBottom == LOW && downUp == 0) {
    timeOut = millis();
    knightRider(35, 2, 0xFF1000); // Cycles, Speed, Width, RGB Color (original orange-red)
  }

  alarmValueTop = digitalRead(alarmPinTop);    // Constantly poll the PIR at the top of the stairs
  //Serial.println(alarmPinTop);
  alarmValueBottom = digitalRead(alarmPinBottom);  // Constantly poll the PIR at the bottom of the stairs
  //Serial.println(alarmPinBottom);

  if (alarmValueTop == HIGH && downUp != 2)  {      // the 2nd term allows timeOut to be contantly reset if one lingers at the top of the stairs before decending but will not allow the bottom PIR to reset timeOut as you decend past it.
    Serial.println(alarmValueTop);
    timeOut = millis(); // Timestamp when the PIR is triggered.  The LED cycle wil then start.
    downUp = 1;
    topdown();         // lights up the strip from top down
  }

  if (alarmValueBottom == HIGH && downUp != 1)  {    // the 2nd term allows timeOut to be contantly reset if one lingers at the bottom of the stairs before decending but will not allow the top PIR to reset timeOut as you decend past it.
    timeOut = millis();  // Timestamp when the PIR is triggered.  The LED cycle wil then start.
    downUp = 2;
    //clearStrip();
    bottomup();         // lights up the strip from bottom up
  }

  if (timeOut + 10000 < millis() && timeOut + 15000 < millis()) { //switch off LED's in the direction of travel.
    if (downUp == 1) {
      colourWipeDown(strip.Color(0, 0, 0), 100); // Off
    }
    if (downUp == 2)  {
      colourWipeUp(strip.Color(0, 0, 0), 100);   // Off
    }
    downUp = 0;
  }
}

void topdown() {
  Serial.println ("detected top");                  // Helpful debug message
  colourWipeDown(strip.Color(255, 255, 250), 25 );  // Warm White
}

void knightRider(uint16_t speed, uint8_t width, uint32_t color) {
  //void knightRider(uint16_t cycles, uint16_t speed, uint8_t width, uint32_t color) {
  uint32_t old_val[23]; // up to 256 lights!

  int forStop = 0;

  if (forStop == 0) {
    //for (int i = 0; i < cycles; i++) {
    //  if (forStop == 1) {
    //   break;
    // }
    for (int count = 0; count < 23; count++) {
      alarmStatus();
      if (alarmValueTop == HIGH && forStop == 0) {
        timeOut = millis();
        downUp = 1;
        forStop = 1;
        topdown();
        break;
      }
      if (alarmValueBottom == HIGH && forStop == 0) {
        timeOut = millis();
        downUp = 2;
        forStop = 1;
        bottomup();
        break;
      }
      strip.setPixelColor(count, color);
      old_val[count] = color;
      for (int x = count; x > 0; x--) {
        old_val[x - 1] = dimColor(old_val[x - 1], width);
        strip.setPixelColor(x - 1, old_val[x - 1]);
      }
      strip.show();
      delay(speed);
    }
    for (int count = 22; count >= 0; count--) {
      if (forStop == 1) {
        break;
      }
      alarmStatus();
      if (alarmValueTop == HIGH && forStop == 0) {
        timeOut = millis();
        downUp = 1;
        forStop = 1;
        topdown();
        break;
      }
      if (alarmValueBottom == HIGH && forStop == 0) {
        timeOut = millis();
        downUp = 2;
        forStop = 1;
        bottomup();
        break;
      }
      strip.setPixelColor(count, color);
      old_val[count] = color;
      for (int x = count; x < 22 ; x++) {
        old_val[x + 1] = dimColor(old_val[x + 1], width);
        strip.setPixelColor(x + 1, old_val[x + 1]);
      }
      strip.show();
      delay(speed);
    }
  }
}
void bottomup() {
  Serial.println ("detected bottom");            // Helpful debug message
  colourWipeUp(strip.Color(255, 255, 250), 25);  // Warm White
}

// Fade light each step strip
void colourWipeDown(uint32_t c, uint16_t wait) {

  for (uint16_t j = 0; j < 13; j++) {
    int start = strip.numPixels() / 13 * j;
    //Serial.println(j);
    for (uint16_t i = start; i < start + 23; i++) {
      strip.setPixelColor(i, c);
    }
    strip.show();
    delay(wait);
  }
}

void clearStrip() {
  for (int l = 0; l < strip.numPixels(); l++) {
    strip.setPixelColor(l, (0, 0, 0));
  }
}
// Fade light each step strip
void colourWipeUp(uint32_t c, uint16_t wait) {
  for (uint16_t j = 13; j > 0; j--) {
    int start = strip.numPixels() / 13 * j;
    //Serial.println(j);
    //start = start-1;
    for (uint16_t i = start; i > start - 23; i--) {
      strip.setPixelColor(i - 1, c);
    }
    strip.show();
    delay(wait);
  }
}

void alarmStatus() {
  alarmValueTop = digitalRead(alarmPinTop);
  alarmValueBottom = digitalRead(alarmPinBottom);

}

void clearStripKnight() {
  for ( int i = 0; i < 25; i++) {
    strip.setPixelColor(i, 0x000000); strip.show();
  }
}

uint32_t dimColor(uint32_t color, uint8_t width) {
  return (((color & 0xFF0000) / width) & 0xFF0000) + (((color & 0x00FF00) / width) & 0x00FF00) + (((color & 0x0000FF) / width) & 0x0000FF);
}

Berichten: 4067
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: Exit function

Berichtdoor shooter » 18 Aug 2020, 20:03

ja kan wel maar je hebt dan geen idee waar je in het progje zit. en break werkt niet met delay samen dus dat blijft toch nog staan.
paul deelen
shooter@home.nl

Berichten: 10
Geregistreerd: 10 Aug 2020, 20:29
Woonplaats: 's-Hertogenbosch (Hintham)

Re: Exit function

Berichtdoor Nicoooootje » 19 Aug 2020, 17:03

Heb intussen veel over Millis zitten lezen maar weet niet goed hoe dit in die functie te krijgen. Hij zal toch door die For moeten en weet niet hoe ik die dan kan afkappen door Millis te gebruiken.

Berichten: 4067
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: Exit function

Berichtdoor shooter » 21 Aug 2020, 08:27

die for even vergeten en iedere keer dat een timer en je teller nog niet vol is even naar een functie springen die += doet en een show
paul deelen
shooter@home.nl

Berichten: 10
Geregistreerd: 10 Aug 2020, 20:29
Woonplaats: 's-Hertogenbosch (Hintham)

Re: Exit function

Berichtdoor Nicoooootje » 02 Okt 2020, 11:35

Heb de Knightrider eruit gehaald dat vond mijn vriendin niet zo leuk.

Voor de gene die het leuk vinden heb ik een video opgenomen om het resultaat te zien

https://www.youtube.com/watch?v=FcSpaAT8fM4

Terug naar Arduino software

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 14 gasten