Go Down

Topic: Toy kitchen stove simulation - slow PWM (Read 810 times) previous topic - next topic

wvmarle

#15
Feb 16, 2018, 04:13 pm Last Edit: Feb 16, 2018, 04:13 pm by wvmarle
The pin handling is correct.

The switch/case statement not: you can not use comparisons in the cases. Must be numbers the variable in switch is compared to. Like this:

Code: [Select]

switch (ledToLight) {
  case 1:
    digitalWrite(led[1], HIGH);
    break;

  case 2:
    digitalWrite(led[2], HIGH);
    break;
}


For what you're trying to do, you have to use regular if/else blocks.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

RustSPb

Here is full code so far, but when compiling I'm getting following error:


exit status 1
'updateMWState' was not declared in this scope


What does it mean? updateMWState is a function...


Code: [Select]

// ----------LIBRARIES--------------



// --------CONSTANTS (Input / Output)---------------

  const byte HP1PotPin = A0, HP2PotPin = A1, OvenPotPin = A3; // the three pot pins
  const int MWButtonPin = 9; // MW On button Pin

  const int HP1LedPin =  13, HP2LedPin = 12, OvenLedPin = 11, MWLedPin = 0, MWBeepPin = 1, MWMotorPin = 10;      // the pin numbers for the LEDs and buzzer
  
  const int pinA = 5; //                            --5--
  const int pinB = 6; //                         4 |     | 6                                                    
  const int pinC = 8; //                           |--2--|                                              
  const int pinD = 3; //                         7 |     | 8                                          
  const int pinE = 7; //                            --3--                                                
  const int pinF = 4;                                                      
  const int pinG = 2;

  const int MWButtonInterval = 300; // number of millisecs between button readings
  const long HPinterval = 5000; // interval at which to blink (milliseconds)
  const int OvenLedShortFadeValue = 10, OvenLedLongFadeValue = 100; // adjust times to suit


//------------ VARIABLES (will change)---------------------

  int HP1PotValue, HP2PotValue, OvenPotValue;
  byte MWButtonState = LOW;

  unsigned long currentMillis = 0;    // stores the value of millis() in each iteration of loop()
  unsigned long previousHP1Millis = 0, previousHP2Millis = 0,previousOvenMillis = 0, previousMWButtonMillis = 0, MWSwitchOnMillis, MWMillisOn;   // will store last time function was updated
  unsigned long OvenFadeOutTime = 10;

  int HP1LedState = LOW, HP2LedState = LOW; // ledState used to set the LED
  int HP1LedOntime = 0, HP2LedOntime = 0, OvenLedOntime = 0; // period within "interval", when Led is on
  int OvenLedValue = 0; // Oven Led ratio

  byte PotThreshold = 50; // pot off threshold

  boolean MWOn = false;

//========================================

void setup() {

  //Serial.begin(9600);
  //Serial.println("Starting SeveralThingsAtTheSameTimeRev1.ino");  // so we know what sketch is running
  
  // set the Stove Led pins as output:
  pinMode(HP1LedPin, OUTPUT);
  pinMode(HP2LedPin, OUTPUT);
  pinMode(OvenLedPin, OUTPUT);
  pinMode(MWLedPin, OUTPUT);
  

  // set the 7seg indicator pins as output:

  pinMode(pinA, OUTPUT);
  pinMode(pinB, OUTPUT);
  pinMode(pinC, OUTPUT);
  pinMode(pinD, OUTPUT);                        
  pinMode(pinE, OUTPUT);
  pinMode(pinF, OUTPUT);
  pinMode(pinG, OUTPUT);
  
  // set the button pin as input with a pullup resistor to ensure it defaults to HIGH
  
  pinMode(MWButtonPin, INPUT);
  
  
 
}

//========================================

void loop() {
 
  readMWButton();               // call the functions that do the work
  updateMWState();
  updateHPStates();
  updateOvenState();
}

//========================================

void readMWButton() {

     // this only reads the button state after the button interval has elapsed
     //  this avoids multiple flashes if the button bounces
     // every time the button is pressed it changes buttonLed_State causing the Led to go on or off
     // Notice that there is no need to synchronize this use of millis() with the flashing Leds
 
 if (millis() - previousMWButtonMillis >= MWButtonInterval) {

   if (digitalRead(MWButtonPin) == HIGH) {
     MWOn = true;            // switching on MW oven
     MWSwitchOnMillis = millis(); // fixing MW on time
     digitalWrite(MWLedPin, HIGH);
     analogWrite(MWMotorPin, 50);
    
     previousMWButtonMillis += MWButtonInterval;
   }
 }

}

//========================================

void UpdateMWState() {

  if (MWOn = true) {
      
    currentMillis = millis();
    MWMillisOn = currentMillis - MWSwitchOnMillis;
    
     if (MWMillisOn < 1000) {
       digitalWrite(pinA, B0);
       digitalWrite(pinB, B0);
       digitalWrite(pinC, B0);
       digitalWrite(pinD, B0);    //9
       digitalWrite(pinE, B1);
       digitalWrite(pinF, B0);
       digitalWrite(pinG, B0);
     break;}

     if (MWMillisOn < 2000){
       digitalWrite(pinA, B0);
       digitalWrite(pinB, B0);
       digitalWrite(pinC, B0);
       digitalWrite(pinD, B0);    //8
       digitalWrite(pinE, B0);
       digitalWrite(pinF, B0);
       digitalWrite(pinG, B0);
     break;}
    
     if (MWMillisOn < 3000){
       digitalWrite(pinA, B0);
       digitalWrite(pinB, B0);
       digitalWrite(pinC, B0);
       digitalWrite(pinD, B1);    //7
       digitalWrite(pinE, B1);
       digitalWrite(pinF, B1);
       digitalWrite(pinG, B1);
     break;}
    
     if (MWMillisOn < 4000){
       digitalWrite(pinA, B0);
       digitalWrite(pinB, B1);
       digitalWrite(pinC, B0);
       digitalWrite(pinD, B0);    //6
       digitalWrite(pinE, B0);
       digitalWrite(pinF, B0);
       digitalWrite(pinG, B0);
     break;}
    
     if (MWMillisOn < 5000){
       digitalWrite(pinA, B0);
       digitalWrite(pinB, B1);
       digitalWrite(pinC, B0);
       digitalWrite(pinD, B0);    //5
       digitalWrite(pinE, B1);
       digitalWrite(pinF, B0);
       digitalWrite(pinG, B0);
     break;}
    
     if (MWMillisOn < 6000){
       digitalWrite(pinA, B1);
       digitalWrite(pinB, B0);
       digitalWrite(pinC, B0);
       digitalWrite(pinD, B1);    //4
       digitalWrite(pinE, B1);
       digitalWrite(pinF, B0);
       digitalWrite(pinG, B0);
     break;}
    
     if (MWMillisOn < 7000){
       digitalWrite(pinA, B0);
       digitalWrite(pinB, B0);
       digitalWrite(pinC, B0);
       digitalWrite(pinD, B0);    //3
       digitalWrite(pinE, B1);
       digitalWrite(pinF, B1);
       digitalWrite(pinG, B0);
     break;}
    
     if (MWMillisOn < 8000){
       digitalWrite(pinA, B0);
       digitalWrite(pinB, B0);
       digitalWrite(pinC, B1);
       digitalWrite(pinD, B0);    //2
       digitalWrite(pinE, B0);
       digitalWrite(pinF, B1);
       digitalWrite(pinG, B0);
     break;}
    
     if (MWMillisOn < 9000){
       digitalWrite(pinA, B1);
       digitalWrite(pinB, B0);
       digitalWrite(pinC, B0);
       digitalWrite(pinD, B1);    //1
       digitalWrite(pinE, B1);
       digitalWrite(pinF, B1);
       digitalWrite(pinG, B1);
     break;}
    
     if (MWMillisOn < 10000){
       digitalWrite(pinA, B0);
       digitalWrite(pinB, B0);
       digitalWrite(pinC, B0);
       digitalWrite(pinD, B0);    //0
       digitalWrite(pinE, B0);
       digitalWrite(pinF, B0);
       digitalWrite(pinG, B1);
     break;}

    if (MWMillisOn >= 10000){
      MWOn = false; // switching off MW oven
      digitalWrite(MWLedPin, LOW);
      analogWrite(MWMotorPin, 0);
      tone(MWBeepPin, 1000, 1000);
       digitalWrite(pinA, B1);
       digitalWrite(pinB, B1);
       digitalWrite(pinC, B1);
       digitalWrite(pinD, B1);    //
       digitalWrite(pinE, B1);
       digitalWrite(pinF, B1);
       digitalWrite(pinG, B1);    
    break;}
  
}
  }
  else {}
}


Wawa

#17
Feb 16, 2018, 11:10 pm Last Edit: Feb 16, 2018, 11:30 pm by Wawa
UpdateMWState (loop) and updateMWState (function).
One with a capital 'U' and the other with a lower case 'u'.
Leo..

RustSPb

#18
Feb 17, 2018, 06:38 am Last Edit: Feb 19, 2018, 07:15 am by RustSPb
Wow!!! Capital/noncapital makes sense!!!

But now same thing with next function inspite I made copy/paste for all names!

exit status 1
'updateHPStates' was not declared in this scope


Here  is balance of the code

Code: [Select]


//========================================

void updateHPStates() {
  
  currentMillis = millis();
  
  HP1potValue = analogRead(HP1potPin); // read pot value for HP1
  HP2potValue = analogRead(HP2potPin); // read pot value for HP2  
  
  HP1LedOntime = map(HP1potValue, 0, 1023, 0, HPinterval); // map pot value to HPinterval
  HP2LedOntime = map(HP2potValue, 0, 1023, 0, HPinterval); // map pot value to HPinterval
  
  if (currentMillis - previousHP1Millis >= HP1LedOntime) HP1ledState = LOW; // unlit hot plate 1 if time came
  if (currentMillis - previousHP2Millis >= HP2LedOntime) HP2ledState = LOW; // unlit hot plate 2 if time came
  
  if (currentMillis - previousHP1Millis >= HPinterval)
  {
    previousHP1Millis = currentMillis;// Start new interval
    if (HP1LedOntime > PotThreshold) HP1LedState = HIGH;
    } // lit hot plate 1 in begining of new intereval if switch is on
  
  if (currentMillis - previousHP2Millis >= HPinterval)
  {
    previousHP2Millis = currentMillis;// Start new interval
    if (HP2LedOntime > PotThreshold) HP2LedState = HIGH;
    } // lit hot plate 2 in begining of new intereval if switch is on
 
  digitalWrite(HP1ledPin, HP1LedState);
  digitalWrite(HP2ledPin, HP2LedState);
}

// ===================================================

void updateOvenState() {
  
  currentMillis = millis(); // update with every loop
  
  OvenPotValue = analogRead(OvenPotPin); // read pot
  OvenLedOntime = map (OvenPotValue, PotThreshold, 1023, OvenLedLongFadeValue, OvenLedShortFadeValue); // map potvalue to fadetime values
  OvenLedOntime = min(OvenLedOntime, OvenLedLongFadeValue); // constrain, pot output not lower than that value
  
  if (OvenPotValue > PotThreshold) { // if stove is turned on
    if (OvenLedValue < 255 && currentMillis - previousOvenMillis >= OvenLedOntime) {
      OvenLedValue++; // increase brightness one step
      analogWrite(OvenLedPin, OvenLedValue); // write to LED
      previousOvenMillis = millis(); // update
    }
  } else { // if stove is turned off
    if (OvenLedValue > 0 && currentMillis - previousOvenMillis >= OvenFadeOutTime) {
      OvenLedValue--; // decrease brightness one step
      analogWrite(OvenLedPin, OvenLedValue); // write to LED
      previousOvenLedMillis = millis(); // update
    }
  }
  
}

wvmarle

It's pretty hard to try out your code if it comes in bits and pieces. There being apparent overlaps between pieces makes it nigh impossible.
Why don't you just post the whole, unaltered sketch in one go, including the complete error message(s)?
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Wawa

#20
Feb 17, 2018, 07:39 am Last Edit: Feb 17, 2018, 07:39 am by Wawa
I don't see any updateHPStates(); or updateOvenState(); function in any of the codes you posted.
The compiler will throw an error if you only have them in loop().
I commented them out in loop(), and removed the break; statements left over from the switch statement.
Removed the empty else at the bottom, and a leftover closing bracket.
Then the sketch compiles (didn't look at anything else).
Leo..
 

RustSPb

Why don't you just post the whole, unaltered sketch in one go, including the complete error message(s)?
I split code because forum told me, that my message too long and I left couple of overlaping strings to show continuation. Sorry, I'm new in programming and now, after your comment I realize, that this rather confusing, than helps. I removed overlaping string now, so second part can be pasted immidiatelly after first part.



I don't see any updateHPStates(); or updateOvenState(); function in any of the codes you posted.

Leo..
 
They are in second part of code above.

I left break because otherwice after first if statement program will go to next, which is also satisfying and I need that it checks secong if only when first is not satisfying. Also I want to save cycles in this long procedure.
As I understood break leads direct out of sub - that's what I need!

Emty else not required?

All code of this sub have to be run only if (MWOn = true), so else {} I wanted to tell arduino to do nothing otherwise - will it be by default?

Wawa

Emty else not required?

All code of this sub have to be run only if (MWOn = true), so else {} I wanted to tell arduino to do nothing otherwise - will it be by default?
Yes. You can have an if() statement, or many if() statements, without an else.

If in doubt about statements, look at the Help>Reference tab of the IDE.
Leo..

wvmarle

I split code because forum told me, that my message too long and I left couple of overlaping strings to show continuation.
In that case:
1) create a minimal sketch that shows the problem (generally also makes it much easier to figure out what's wrong - fair chance you find it out yourself before even posting), or that failing
2) add the complete sketch as attachment.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

RustSPb

Full code un attachment, Error message:

Arduino: 1.6.11 (Windows 10), Плата:"Arduino/Genuino Uno"

D:\arduino-1.6.11\arduino-builder -dump-prefs -logger=machine -hardware D:\arduino-1.6.11\hardware -tools D:\arduino-1.6.11\tools-builder -tools D:\arduino-1.6.11\hardware\tools\avr -built-in-libraries D:\arduino-1.6.11\libraries -libraries D:\My Documents\Arduino\libraries -fqbn=arduino:avr:uno -ide-version=10611 -build-path C:\Users\admin\AppData\Local\Temp\build552377128ba4fcd0829c2f2cd21c709f.tmp -warnings=none -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avrdude.path=D:\arduino-1.6.11\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=D:\arduino-1.6.11\hardware\tools\avr -verbose D:\Desktop\Kitchen\Kitchen.ino
D:\arduino-1.6.11\arduino-builder -compile -logger=machine -hardware D:\arduino-1.6.11\hardware -tools D:\arduino-1.6.11\tools-builder -tools D:\arduino-1.6.11\hardware\tools\avr -built-in-libraries D:\arduino-1.6.11\libraries -libraries D:\My Documents\Arduino\libraries -fqbn=arduino:avr:uno -ide-version=10611 -build-path C:\Users\admin\AppData\Local\Temp\build552377128ba4fcd0829c2f2cd21c709f.tmp -warnings=none -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avrdude.path=D:\arduino-1.6.11\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=D:\arduino-1.6.11\hardware\tools\avr -verbose D:\Desktop\Kitchen\Kitchen.ino
Using board 'uno' from platform in folder: D:\arduino-1.6.11\hardware\arduino\avr
Using core 'arduino' from platform in folder: D:\arduino-1.6.11\hardware\arduino\avr
Detecting libraries used...
"D:\arduino-1.6.11\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics  -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10611 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR   "-ID:\arduino-1.6.11\hardware\arduino\avr\cores\arduino" "-ID:\arduino-1.6.11\hardware\arduino\avr\variants\standard" "C:\Users\admin\AppData\Local\Temp\build552377128ba4fcd0829c2f2cd21c709f.tmp\sketch\Kitchen.ino.cpp" -o "nul"
"D:\arduino-1.6.11\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics  -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10611 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR   "-ID:\arduino-1.6.11\hardware\arduino\avr\cores\arduino" "-ID:\arduino-1.6.11\hardware\arduino\avr\variants\standard" "C:\Users\admin\AppData\Local\Temp\build552377128ba4fcd0829c2f2cd21c709f.tmp\sketch\Kitchen.ino.cpp" -o "nul"
Generating function prototypes...
"D:\arduino-1.6.11\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics  -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10611 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR   "-ID:\arduino-1.6.11\hardware\arduino\avr\cores\arduino" "-ID:\arduino-1.6.11\hardware\arduino\avr\variants\standard" "C:\Users\admin\AppData\Local\Temp\build552377128ba4fcd0829c2f2cd21c709f.tmp\sketch\Kitchen.ino.cpp" -o "C:\Users\admin\AppData\Local\Temp\build552377128ba4fcd0829c2f2cd21c709f.tmp\preproc\ctags_target_for_gcc_minus_e.cpp"
"D:\arduino-1.6.11\tools-builder\ctags\5.8-arduino10/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\Users\admin\AppData\Local\Temp\build552377128ba4fcd0829c2f2cd21c709f.tmp\preproc\ctags_target_for_gcc_minus_e.cpp"
Компиляция скетча...
"D:\arduino-1.6.11\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10611 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR   "-ID:\arduino-1.6.11\hardware\arduino\avr\cores\arduino" "-ID:\arduino-1.6.11\hardware\arduino\avr\variants\standard" "C:\Users\admin\AppData\Local\Temp\build552377128ba4fcd0829c2f2cd21c709f.tmp\sketch\Kitchen.ino.cpp" -o "C:\Users\admin\AppData\Local\Temp\build552377128ba4fcd0829c2f2cd21c709f.tmp\sketch\Kitchen.ino.cpp.o"
D:\Desktop\Kitchen\Kitchen.ino: In function 'void loop()':

Kitchen:78: error: 'updateHPStates' was not declared in this scope

   updateHPStates();

                  ^

Kitchen:79: error: 'updateOvenState' was not declared in this scope

   updateOvenState();

                   ^

D:\Desktop\Kitchen\Kitchen.ino: In function 'void updateMWState()':

Kitchen:118: error: break statement not within loop or switch

      break;}

      ^

Kitchen:128: error: break statement not within loop or switch

      break;}

      ^

Kitchen:138: error: break statement not within loop or switch

      break;}

      ^

Kitchen:148: error: break statement not within loop or switch

      break;}

      ^

Kitchen:158: error: break statement not within loop or switch

      break;}

      ^

Kitchen:168: error: break statement not within loop or switch

      break;}

      ^

Kitchen:178: error: break statement not within loop or switch

      break;}

      ^

Kitchen:188: error: break statement not within loop or switch

      break;}

      ^

Kitchen:198: error: break statement not within loop or switch

      break;}

      ^

Kitchen:230: error: a function-definition is not allowed here before '{' token

 void updateHPStates() {

                       ^

Kitchen:283: error: expected '}' at end of input

 }

 ^

exit status 1
'updateHPStates' was not declared in this scope

wvmarle

Quote
Kitchen:283: error: expected '}' at end of input
There's your problem, which confirms what I guess from the other error messages.

There's a } missing somewhere.

Use the autoformat function of the IDE (press ctrl-T) and you'll quite quickly be able to find this: somewhere in the first 280 lines of your sketch the indentation will go off.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Go Up