HELP!

Projecten die niet passen in bovenstaande onderwerpen
Berichten: 4
Geregistreerd: 25 Mrt 2019, 15:06

HELP!

Berichtdoor arduino_noob96 » 25 Mrt 2019, 15:20

Ik heb hulp nodig met mijn Arduino code.
Ik probeer een code te schrijven waarbij ik een LED, Potentiometer en drukknop gebruik. Het idee luidt als volgt:
- De LED begint in een stand waarbij hij geen licht geeft
*Drukt op knop*
- De LED gaat aan en geeft constant de kleur blauw
*Drukt op knop*
- De LED vervaagt van blauw naar uit naar blauw enz.
*Drukt op knop*
- De led wordt 1 seconde groen, en gaat vervolgens uit
*Drukt op knop*
- De LED is rood en de helderheid wordt bepaald door de Potentiometer
*Drukt op knop*
- De LED gaat weer uit

Het zijn in principe 5 animaties waaruit de code bestaat.

Wie o wie kan mij hierbij helpen

Mijn begin staat hieronder

Code: Alles selecteren
#include <FastLED.h>

//connect the potentiometer to pin A0
int potPin = A0;

//connections of the ChainableLED
#define DATA_PIN 5
#define CLOCK_PIN 4

//how many leds
#define NUM_LEDS 1

//an array to hold the led data
CRGB leds[NUM_LEDS];

CRGB blue = 0x006AFF;

//how long is the animation
int animationDuration = 2000;
//keep track of last time we updated the led brightness
long lastLedUpdateTime = 0;
//how many steps has the animation
int animationSteps = 40;
//how often are we supposed to change the color of the led
float animationUpdateInterval = animationDuration/animationSteps;
//is our animation moving forward or backward
int animationDirection = 1;
//the maximum & minimum brightness, mainy needed for fade animation
int maxBrightness = 255;
int minBrightness = 0;
//how much do we change the color at every step of the animation
float incrementAmountXStep = (maxBrightness - minBrightness) / animationSteps;
//the led brightness
int brightness;
int color_hue;

//button
int buttonPin = 2;
//the value that the button had in the previous loop()
boolean lastButtonPressed;

//multiple types of animation
enum animationType {
  SINGLE_COLOR,
  FADE_SINGLE_COLOR,
  BLINK_SINLE_COLOR,
  BRIGHTNESS_RED,
  NONE,
};

//the animation currently active
animationType currentAnimation=NONE;
//how many animations do we have
int numAnimations=5;

void setup() {
  //start the led library
  FastLED.addLeds<P9813, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
}

void loop() {
  //if the button is pressed we change animation
  if (readButtonA()) {
    //check that we are within the range of the the animations we programmed
    if (currentAnimation<numAnimations-1) {
      //since currentAnimation is a ENUM we cannot simply do currentAnimation++
      currentAnimation = (animationType)(currentAnimation + 1);
    }else{
      currentAnimation = 0;
    }

    // start the right animation
    switch (currentAnimation) {
    case SINGLE_COLOR:
      startSingleColor();
      break;
    case FADE_SINGLE_COLOR:
      startFadeAnimation(2000,100);
      break;
    case BLINK_SINLE_COLOR:
      //we can still use the initFadeAnimation setting the number of steps to 1
      startFadeAnimation(10000,1);
      break;
    case BRIGHTNESS_RED:
      startSingleColor();
      break;
    case NONE:
      stopAnimations();
      break;
    }
  }

  //if the animation is not of type rainbox set the color using the potentiometer
  if (currentAnimation!=BRIGHTNESS_RED) {
    float potValue = analogRead(potPin);
    brightness = map(potValue, 0,1024,0,255);
  }

  //timing checks and led update is done inside this function
  updateLedAnimation();
}

//this function returns true when the button is pressed
boolean readButtonA(){
  boolean buttonStatus=false;
  boolean buttonPressed=digitalRead(buttonPin);
 
  //if the button is now down and the last time through the loop() is was not
  if (buttonPressed && !lastButtonPressed) {
    buttonStatus=true;
  }
  lastButtonPressed=buttonPressed;
  return(buttonStatus);
}

void startFadeAnimation(int animationDuration, int animationSteps){
  animationSteps=animationSteps;
  animationDuration=animationDuration;

  //recall the steps and the intervals
  animationUpdateInterval=animationDuration/animationSteps;
  incrementAmountXStep = (maxBrightness - minBrightness) / animationSteps;
}

void stopAnimations(){
  //turn off
  brightness=0;
}

void startSingleColor() {
  //set the brightness to the max
  brightness=maxBrightness;
}

void updateFadeAnimation() {
  //increment the value of brightness
  brightness = brightness + incrementAmountXStep * animationDirection;
  //check that we havn't reached the extreme
  if (brightness > maxBrightness || brightness < minBrightness) {
    animationDirection *= -1;
    //make sure the value falls between the desired range
    brightness = constrain(brightness,minBrightness, maxBrightness);
  }
}

void startRainbowAnimation(int rainbowDuration, int rainbowSteps){
  color_hue = 0;
  brightness = maxBrightness;
  animationSteps = rainbowSteps;
  animationDuration = rainbowDuration;

  animationUpdateInterval = animationDuration/animationSteps;
  incrementAmountXStep = 255 / animationSteps;
}

void updateRainbowAnimation() {
  //increment the hue
  color_hue += incrementAmountXStep*animationDirection;
  //set the brightness to max
  brightness = maxBrightness;
  //reset the hue to 0 when we reach 1
  if (color_hue > 255) {
    color_hue = 0;
  }
}

void updateLedAnimation() {
  if (millis() - lastLedUpdateTime > animationUpdateInterval) {
    switch (currentAnimation) {
    case NONE:
      break;
     
    case FADE:
      updateFadeAnimation();
      break;

    case BLINK:
      updateFadeAnimation();
      break;

    case RAINBOW:
      updateRainbowAnimation();
      break;
    }
    lastLedUpdateTime=millis();
  }

  //update the led color
  leds[0].setHSV(color_hue, 255, brightness);
  FastLED.show();
}

Advertisement

Gebruikers-avatar
Berichten: 2655
Geregistreerd: 06 Aug 2016, 01:03

Re: HELP!

Berichtdoor Koepel » 25 Mrt 2019, 16:43

Hallo, welkom op dit forum.

Je bent inderdaad goed vastgelopen. Als je zo door gaat, dan loop je steeds meer vast.
Je hebt verschillende "states" voor de verschillende animaties, dat is niet toereikend. Je hebt verschillende "states" nodig voor de verschillende toestanden waarin de sketch zich kan bevinden. Dat komt misschien op hetzelfde neer, maar het gaat om het idee.

De oplossing is om terug te keren naar de standaard manier van Arduino en een finite state machine.
Dit is uitleg om mee te beginnen: https://majenko.co.uk/blog/finite-state-machine.

Je hebt een sketch met een finite state machine. Dan is het handig als de finite state machine het belangrijkste deel van de sketch wordt, die altijd wordt uitgevoerd.

De state "NONE" is eigenlijk je default, de rust-stand. Die kun je misschien vooraan zetten.

Ik geef er de voorkeur aan om eerst alle informatie van knoppen, sensoren en potentiometer te verzamelen voordat de finite state machine wordt uitgevoerd. Dat betekend bijvoorbeeld altijd de potentiometer inlezen, ongeacht of je die gaat gebruiken of niet. Dat heeft als voordeel dat je in de finite state machine eenvoudiger de beslissingen kunt nemen, omdat altijd alle informatie beschikbaar is.

Het faden van een led doe je het best buiten de finite state machine met millis(), maar de finite state machine moet het faden wel op ieder moment kunnen aan- en uitzetten.

Ik heb hier drie finite state machines tussen zitten: https://github.com/Koepel/Fun_with_millis.
Kijk eens naar "millis_and_finite_state_machine.ino", dan zie je alles wat ik hierboven schrijf.
Het faden van een led kun je zien in "millis_soft_pulsating_led.ino", ik vind die zelf erg goed gelukt ;)

Dus:
- Altijd de finite state machine uitvoeren. Dat is de kapstok waar de rest onder valt.
- Vooraf eerst alle gegevens verzamelen, zodat je in de finite state machine de juiste beslissingen kunt nemen.
- Een led faden of knipperen met millis() buiten de finite state machine.

Berichten: 4
Geregistreerd: 25 Mrt 2019, 15:06

Re: HELP!

Berichtdoor arduino_noob96 » 25 Mrt 2019, 18:39

Beste Koepel,

Heel erg bedankt voor je reactie, echter snap ik alsnog niet helemaal wat ik moet doen nu. Ik ben nog maar een beginner met Arduino, waardoor ik heel veel dingen nog niet begrijp. Ik vroeg me af of jij, of iemand anders, mij kan helpen met deze code schrijven. Ik wil mijn broertje ermee verrassen voor zijn verjaardag. Hij is verstandelijk gehandicapt en het idee is dat het device in een soort armband komt die ik voor hem heb gemaakt.

Ik hoor graag van je

Berichten: 4
Geregistreerd: 25 Mrt 2019, 15:06

Re: HELP!

Berichtdoor arduino_noob96 » 25 Mrt 2019, 18:41

Ik gebruik trouwens een Seeeduino lotus bord met de potentiometer in A0, de RGB LED in D4 en de drukknop in D2

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

Re: HELP!

Berichtdoor shooter » 25 Mrt 2019, 19:41

Waar loop je vast? want een gecopieerd programma is toch nooit wat jij wil.
paul deelen
shooter@home.nl

Berichten: 4
Geregistreerd: 25 Mrt 2019, 15:06

Re: HELP!

Berichtdoor arduino_noob96 » 25 Mrt 2019, 19:55

De code die ik bij het bericht geplaatst heb, heb ik online gevonden. Ik loop vast bij het veranderen van deze code naar mijn gewenste uitkomst. Persoonlijk heb ik niet echt leerdoelen hierbij, ik probeer het vooral voor mijn broertje te doen, zodat hij iets heel bijzonders heeft.

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

Re: HELP!

Berichtdoor shooter » 26 Mrt 2019, 23:15

tot aan de loop niets veranderen.
ook onder de loop even niets wijzigen.
kijk dan eens of je het voorbeeld wat kunt versimpelen. dus probeer elke stap te begrijpen wat er gebeurt.
zet de rest even tussen /* en */ dan kun je zien wat ieder stuk doet.
schrijf er zelf commentaar bij.
zorg dat je vaak een backup maakt dan kun je makkelijk een stap terug.
en ja ik weet dat het veel werk is, je kunt ook proberen simpel te beginnen.
paul deelen
shooter@home.nl

Gebruikers-avatar
Berichten: 2655
Geregistreerd: 06 Aug 2016, 01:03

Re: HELP!

Berichtdoor Koepel » 27 Mrt 2019, 05:30

Hallo arduino_noob96,
Arduino is om te leren programmeren, om snel iets werkend te krijgen, en omdat het leuk is.
Op dit forum schrijven we liever niet een complete sketch voor iemand. Dan kan diegene zelf geen wijzigingen aanbrengen en blijft afhankelijk van anderen.

Ik dacht dat je die code zelf had geschreven, en dat je voldoende kennis van programmeren had om er een "echte" finite state machine van te maken.

Het Seeeduino Lotus board lijkt me prima. Het is te vergelijken met een Arduino Uno board.
http://wiki.seeedstudio.com/Seeeduino_Lotus/

We begrijpen je goede bedoelingen, maar in de praktijk blijkt het niet te werken als wij een complete sketch geven.
Ik heb volgens mij een RGB NeoPixel led ergens in een project zitten, maar die zit vastgesoldeerd. Dus ik kan geen code voor je uitproberen.

De sketch die je hebt gevonden, die heeft geen duidelijke structuur, geen duidelijk opbouw van de code.
Waar heb je die gevonden ?
Kun je die sketch gebruiken zoals het nu is ?
Om dingen te wijzigen is het handig als je ervaring hebt met programmeren. De speciale dingen voor Arduino kun je vanzelfsprekend aan ons vragen, en alles staat ook online: https://www.arduino.cc/reference/en/.

Terug naar Overige projecten

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 14 gasten