switch case met delay

Arduino specifieke Software
Berichten: 3
Geregistreerd: 17 Feb 2018, 21:46

switch case met delay

Berichtdoor wijnand » 17 Feb 2018, 22:03

Hallo allemaal,
Ik ben nieuw op het forum en heb een vraag hoe elegant een pauze in te lassen in een switch met verschillende onderliggende cases. Ik kom hier niet uit. De code stuurt een lader aan die de stroom regelt door het drukken op een knop. Bij iedere knop actie wordt de stroom verhoogd. Op zich werkt dit allemaal.
Wat ik nu wil is dat er bij een bepaalde case 4 seconden gewacht wordt. Dit omdat die extrene ook hardware triggerd die niet te snel aan/uit geschakeld mag worden. De code op zich werkt goed alleen bij case 6 wil ik dat ie 4 seconden wacht voordat je door kan drukken naar de volgende case. Ook al druk je op de knop..
Nu heb ik dit geprobeert op te lossen met een delay, maar dat is niet handig gebleken omdat de Arduino dan gewoon 4 seconden in de wacht staat..

Iemand een idee? Alvast bedankt voor het meedenken!


Stukje uit de code van de switch case:

if((digitalRead(buttonup)==0)) // read digital pin for button up and if it is low (pushbutton is pressed)
{
digitalWrite(led,HIGH); // turn the LED on (indicating a button pressed)
currentstep = currentstep + 1;
switch (currentstep)
{
case 0: // currentstep is now transformed into a step counter for different steps in current and number of phases/circuits
currentsetpoint = 0; // first (default step) is 0 Amps
onephaseorthreephase = 1; // and single phase
break;
// case 1:
// currentsetpoint = 6 * 10; // 6 amps on a single phase (including the onboard charger) FP input amps will be around 0.5A (0.2A each) Total power is around 1.5 kW
// onephaseorthreephase = 1;
// break;
case 1:
currentsetpoint = 10 * 10; // 10 amps on a single phase (incl onboard). This step is for (13/16A) circuits with other equipment on it. FP input amps will be around 4.5A (1.5A each). Total power is around 2.2 kW
onephaseorthreephase = 1;
break;
case 2:
currentsetpoint = 13 * 10; // 13 amps on a single phase (incl onboard). This step is for (13A) circuits with no other equipment on it or 16A circuit with some small equipment (fridge/TV etc). FP input amps will be around 7.5A (2.5A each). Total power is around 3 kW
onephaseorthreephase = 1;
break;
case 3:
currentsetpoint = 16 * 10; // 16 amps on a single phase (incl onboard). This step is for (16A) circuits with no other equipment on it. FP input amps will be around 10.5A (3.5A each). Total power is around 3.5 kW
onephaseorthreephase = 1;
break;
case 4:
currentsetpoint = 16 * 10; // 16 amps on 2 circuits, where 1 circuit is used for the onboard and the 2nd circuit for the FP's. FP amps will be around 16A (5.3A each). Can also be used for a single circuit if the onboard is not used. Total power is around 3.5 kW without onboard and 4.8 kW with onboard
onephaseorthreephase = 2;
break;
case 5:
currentsetpoint = 16 * 10; // 16 amps on a three phase circuit where the onboard is on phase one, and all 3 sets of FP on each phase so one phase has both a FP and the onboard on it. FP amps will be around 10.5 A each Used for 3Ph 11kW charging stations. Total power is around 7.2 kW without onboard and 8.5 kW with onboard
onephaseorthreephase = 4;
break;
case 6:
currentsetpoint = 16 * 10; // 16 amps on a three phase circuit where the onboard is DISCONNECTED!
onephaseorthreephase = 3;
digitalWrite(relay,HIGH);
// delay(4000); //delay to switch off onboard for at least 4 secs before switch on again
break;
// case 8:
// currentsetpoint = 16 * 10; // 16 amps on a threephase circuit. This should supply full power of a 11 kW chargepoint. Use this WITHOUT the onboard and a controller connection as the current will exceed 100A (max 112.5A). Also not to be used for an 11.4 kWh pack, only for 13.0 and up (116 Ah)
// onephaseorthreephase = 3; // if you do not own a 13.0 or higher kWH Zero Pack leave these 4 lines commented out (case 8: until break;). this is for security that you do not exceed 1C
// break;
default:
digitalWrite(relay,LOW);
currentsetpoint = 0; // return to the first step of 0 current.
currentstep = 0;
onephaseorthreephase =1;
break;
}
delay(200); // debouncing time
digitalWrite(led,LOW); // turn the LED off



}

Advertisement

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

Re: switch case met delay

Berichtdoor Koepel » 17 Feb 2018, 22:22

Hallo en welkom.

Kun je aangeven of je ervaring hebt met programmeren ?

Boven het tekst veld op dit forum is er een knop "Code" en een drop-downlist "Select a Syntax" en dan "C++". Als je code binnen die code tags zet, dan is het beter leesbaar. Wij hebben ook graag een volledige sketch.

Een delay geeft niets, als de Arduino niets anders te doen heeft.
Wil je wel nog van alles doen, begin dan met BlinkWithoutDelay: https://www.arduino.cc/en/tutorial/BlinkWithoutDelay.

Ik programmeer een beetje op gevoel en zie de afzonderlijke brokjes voor me en ik denk in datastromen. Dus dan zie ik een conflict bij de invoer. De knop zie ik namelijk als een data invoer. Om te beginnen zou ik een anti-bounce library gebruiken. Bijvoorbeeld "Bounce2", die zit in the Library Manager (Bibliotheek Beheerder). Het deel van de knop detecteren hoort in een afzonderlijk deel.

Met behulp van millis() is ook een eenmalige software timer te maken. Zolang die loopt wordt dan de knop niet verwerkt.

Er is nog een goede beschrijving nodig.
Stel dat de knop blijvend wordt ingedrukt, in plaats van eerst los te laten. Wil je dan dat het na 4 seconden toch geaccepteerd wordt ? Of heb je nu al iets waardoor het automatisch omhoog gaat als iemand de knop ingedrukt houdt ? als een soort auto-repeat ?
Om dat correct te beschrijven is lastig. Probeer het eens.

Berichten: 3
Geregistreerd: 17 Feb 2018, 21:46

Re: switch case met delay

Berichtdoor wijnand » 17 Feb 2018, 23:53

Dank voor je reactie, ik zal de code knop eens gebruiken, idd wel zo overzichtelijk, ziet er zo al veel beter uit ;-)
Ik heb enige programmeer ervaring, heb de totale code bij elkaar geknipt en geplakt met behulp van deze en gene en wat logisch nadenkwerk..

Probleem met de delay nu is dat de Arduino nu dus 4 sec. niets doet. Dat lijkt niet erg, maar ondertussen moet ie wel andere taken uitvoeren. (om de seconde via CAN messages inloggen op de laders, anders vallen die terug naar hun default waardes) En dat gebeurt dus nu. Vandaar dat ik 'm nu ook even heb uitgekommentarieerd in de code.

De delay zou een veiligheid moeten zijn die te snel over case 6 stappen zou moeten voorkomen. Ook al druk je op de knop, hij moet wachten tot die 4 seconden voorbij zijn en dan mag ie door, terug naar het begin van de loop. Op zich werkt de delay dus prima, al moet ik zoeken naar eenzelfde effect zonder "pauze" effect op de Arduino..
Ik lees iets over om een aparte variabele op te slaan en dan steeds te vergelijken of de 4 seconden al verstreken zijn. Maar dat zegt mij nog niets. Eens verder lezen..


cpp code
Code: Alles selecteren
      if((digitalRead(buttonup)==0))                                                // read digital pin for button up and if it is low (pushbutton is pressed)
        {
          digitalWrite(led,HIGH);                                                   // turn the LED on (indicating a button pressed)
            currentstep = currentstep + 1;
            switch (currentstep)
            {
              case 0:                                                               // currentstep is now transformed into a step counter for different steps in current and number of phases/circuits
              currentsetpoint = 0;                                                  // first (default step) is 0 Amps
              onephaseorthreephase = 1;                                             // and single phase
              break;
//              case 1:
//              currentsetpoint = 6 * 10;                                             // 6 amps on a single phase (including the onboard charger) FP input amps will be around 0.5A (0.2A each) Total power is around 1.5 kW
//              onephaseorthreephase = 1;
//              break;
              case 1:
              currentsetpoint = 10 * 10;                                            // 10 amps on a single phase (incl onboard). This step is for (13/16A) circuits with other equipment on it. FP input amps will be around 4.5A (1.5A each). Total power is around 2.2 kW
              onephaseorthreephase = 1;
              break;
              case 2:
              currentsetpoint = 13 * 10;                                            // 13 amps on a single phase (incl onboard). This step is for (13A) circuits with no other equipment on it or 16A circuit with some small equipment (fridge/TV etc). FP input amps will be around 7.5A (2.5A each). Total power is around 3 kW
              onephaseorthreephase = 1;
              break;
              case 3:
              currentsetpoint = 16 * 10;                                            // 16 amps on a single phase (incl onboard). This step is for (16A) circuits with no other equipment on it. FP input amps will be around 10.5A (3.5A each). Total power is around 3.5 kW
              onephaseorthreephase = 1;
              break;
              case 4:
              currentsetpoint = 16 * 10;                                            // 16 amps on 2 circuits, where 1 circuit is used for the onboard and the 2nd circuit for the FP's. FP amps will be around 16A (5.3A each). Can also be used for a single circuit if the onboard is not used. Total power is around 3.5 kW without onboard and 4.8 kW with onboard
              onephaseorthreephase = 2;
              break;
              case 5:
              currentsetpoint = 16 * 10;                                            // 16 amps on a three phase circuit where the onboard is on phase one, and all 3 sets of FP on each phase so one phase has both a FP and the onboard on it. FP amps will be around 10.5 A each Used for 3Ph 11kW charging stations. Total power is around 7.2 kW without onboard and 8.5 kW with onboard
              onephaseorthreephase = 4;
              break;
              case 6:
              currentsetpoint = 16 * 10;                                            // 16 amps on a three phase circuit where the onboard is DISCONNECTED!
              onephaseorthreephase = 3;
              digitalWrite(relay,HIGH);
//              delay(4000);                                                          //delay to switch off onboard for at least 4 secs before switch on again
              break;
//              case 8:
//              currentsetpoint = 16 * 10;                                            // 16 amps on a threephase circuit. This should supply full power of a 11 kW chargepoint. Use this WITHOUT the onboard and a controller connection as the current will exceed 100A (max 112.5A). Also not to be used for an 11.4 kWh pack, only for 13.0 and up (116 Ah)
//              onephaseorthreephase = 3;                                             // if you do not own a 13.0 or higher kWH Zero Pack leave these 4 lines commented out (case 8: until break;). this is for security that you do not exceed 1C
//              break;
              default:
              digitalWrite(relay,LOW);
              currentsetpoint = 0;                                                  // return to the first step of 0 current.
              currentstep = 0;
              onephaseorthreephase =1;
              break;             
            }
          delay(200);                                                               // debouncing time
          digitalWrite(led,LOW);                                                    // turn the LED off

       
       
        }

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

Re: switch case met delay

Berichtdoor shooter » 18 Feb 2018, 14:55

je moet nu van case naar case stappen omdat je er een currentstep gebruikt, maar je mag ook een next step gebruiken, dan kun je naar elke case springen die je wil.
in die case komt een timer, en als de timer afgelopen is dan wordt next step 5 anders blijft deze 4.
zo kun je dus hele beslisbomen maken met allerlei voorwaarden.
paul deelen
shooter@home.nl

Berichten: 3
Geregistreerd: 17 Feb 2018, 21:46

Re: switch case met delay

Berichtdoor wijnand » 18 Feb 2018, 20:37

Dat zou kunnen idd. Maar ik wil juist in de volgorde van de case's blijven. Dit omdat de stroomsterkte steeds toeneemt in de stappen en het bullet (idiot) proof moet zijn en met 1 knop te bedienen.

Terug naar Arduino software

Wie is er online?

Gebruikers in dit forum: Alexduh, husayzopefodi, MiltonHudge, ocisitila en 20 gasten