7
\$\begingroup\$

I have a project in embedded systems and I have to write the code in C, and make it simple.

This is my first time, and it's only a simulation of how it should work.

#include<iostream.h>
#include<stdio.h>

void manualMode();
 void defaultMode();

int temp, timeToWash, operation;

int main() {
     int mode =0, part=3, waterLevel;
     int doorSensor=0, timer = 3,waterLevelSensor = 0, tempSensor = 25;
     char startPause;
     cout<<"Washing Machine CE224"<<endl;
 do{
      cout<<"Choose Mode: (1)Default, (2)Manual \n";
      cin>>mode;
      if (mode == 1)
                 defaultMode();
      if (mode == 2)
                 manualMode();
 }while(mode == 0);

 cout<<"Choose Parts to wash: (1)Upper Only, (2)Lower Only, (3)Upper and Lower \n";
 cin>>part;
 switch(part){
            case 1:
            case 2:
                 waterLevel= 0.5;
                 break;
            case 3:
                 waterLevel = 1;
                 break;
 }
 if (doorSensor == 0){
      do{
                 cout<<"Press (S) to Start,and close the Door."<<endl;
                 cin>>startPause;
                 cout<<"LED is On\n";
                 while(waterLevelSensor != waterLevel){
                             waterLevelSensor++;
                 }
                 while(tempSensor != temp){
                             tempSensor++;
                 }
                 cout<<"Washing Operation Started!               Time Left:"<<timeToWash<<endl;
                 cout<<"Washing with Soap Operation Started!\n";
                 timeToWash = timeToWash / 2 ;
                 cout<<"Rinsing Operation Started!               Time Left:"<<timeToWash<<endl;
                 timeToWash = timeToWash / 2 ;
                 cout<<"Drying Operation Started!               Time Left:"<<timeToWash<<endl;
                 timeToWash = 0 ;
                 startPause = 'P';
      }while((startPause == 's') || (startPause =='S'));
 }
 cout<<"End!\nLED is Off";
 return 0;
}


void manualMode(){
     cout<<"Enter Temperature: \n";
     cin>>temp;
     cout<<"Enter Time to wash: \n";
     cin>>timeToWash;
     cout<<"Choose Operation: (1)Water Only, (2)Water and Soap, (3)Rinse, (4)Dry, (5)All\n";
     cin>>operation;
}
void defaultMode(){
     int whatToWash;
     cout<<"Choose what to Wash: (1)Cups, (2)Plates, (3)Pots";
     cin>>whatToWash;
     switch(whatToWash){
          case 1:
                temp = 30;
                timeToWash = 3;
                break;
          case 2:
                temp = 40;
                timeToWash = 3;
                break;
          case 3:
                temp = 60;
                timeToWash = 3;
                break;
     }
 }
\$\endgroup\$
  • 2
    \$\begingroup\$ C++ != C. Which is it? \$\endgroup\$ – user34073 Jan 31 '15 at 21:50
  • \$\begingroup\$ C with cout and cin . \$\endgroup\$ – user64183 Jan 31 '15 at 21:51
  • 1
    \$\begingroup\$ Then I'll retag it as c++. \$\endgroup\$ – 200_success Jan 31 '15 at 21:52
  • \$\begingroup\$ Did you have a specific kind of embedded system in mind? \$\endgroup\$ – 200_success Jan 31 '15 at 21:53
  • \$\begingroup\$ We only studied the PIC16F877A micro controller but it doesn't matter, we're only asked to make simulation of it. \$\endgroup\$ – user64183 Jan 31 '15 at 21:55
4
\$\begingroup\$

I would have some suggestions for improvements regarding C++ in general (I'm not familiar with embedded systems).

Indentation

Make sure to use uniform indentation across the code, in order to make it more readable. (E.g., the do-while loop, the switch-block in the main method should be at the same level as the declarations at the beginning.)

Code organization

It is very good, that manual mode and default mode are two separate functions. Breaking down the code makes it easier to read. I would also export some other parts to separate functions, especially the main cycle. (Remark: this is a generic principle. It might be that in the context of embedded systems there are some further considerations, which I'm not aware of, e.g. that code should be as fast as possible, for which this advice does not apply. Please consider this.)

Variable initialization

I would initialize each variable explicitly to some default value (e.g. 0), even if it is sure to be initialized later. In this way, you can for sure prevent undefined behavior (e.g., in case the code gets modified later, and a path is introduced where the variable is not initialized anymore).

Control flow

In the do-while loop for the washing-cycle, startPause is read, but then, at the end of the loop, it is set to a constant value (independently of what has been read). So this loop only runs once. I think either the whole logic should be removed, or improved in a way that looping really depends on the input value.

Input validation

In more places, input is required from the user, but it is never checked if it is within the range of the allowed values. In a production system this would be a must, but also in your case, I would clarify if this is required.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thank you very much.. I'll work with your suggestions ! they're very helpful ! thanks ^_^ \$\endgroup\$ – user64183 Jan 31 '15 at 22:41
2
\$\begingroup\$

I spotted

int mode=0, part=3, waterLevel;

and

switch(part){
  case 1:
  case 2:
    waterLevel=0.5;
    break;

which means the machine will try to wash without ANY water (as int(0.5)==0) when the user selects "1/2 load". While environmentally friendly due to the low water (and heating power) consumption, I'm not satisfied with the cleaning results.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.