If I wanted to add an emergency stop to an elevator programme should I use an if statement? Eg
Or something along those lines?Code:If (emergency stop) { Void main(); }
If I wanted to add an emergency stop to an elevator programme should I use an if statement? Eg
Or something along those lines?Code:If (emergency stop) { Void main(); }
There are many ways to do it. An if statement can do the job, but why you have void main there? You would probably want to use the exit function.
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
Stopping an elevator is also a markedly different concept from stopping the program that controls it.
Just put void main there as an example, should I put void and the name of my programme?
No.
When we want to terminate a program because something went wrong, I would do this.
If I was in the main, then I would do this
However, as you know, we use functions in programs and as a result, it is helpful to use exit function.Code:int main(void) { .... if(mustTerminate) return -1; // this usually shows that we did not terminate successfully // DO WORK // Everything ok, thus return a zero return 0; }
BUT, consider reading again what grumpy said!!Code:... void someFunction(void) { if(somethingWentTerriblyWrong) exit(1); }
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
No. Try it and see .... the compiler will complain bitterly and refuse to compile your program.
Generally speaking, calling main() explicitly is usually a bad idea. There are certain cases where it is useful but, in practice, most programmers who try to call main() expect it to work differently than it actually does.
And, if your C compiler is really a C++ compiler, calling main() is not allowed either.
How about calling a function called say
StopMotor();
Then go onto the usual thing of waiting for another keypress from the elevator control panel (intercom, open doors, raise panic alarm).
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Cheers guys, I'll give it a go at writing it either tonight or tomorrow after work and upload to see what you think and what could be improved
this is what ive written so far, im using a 89s2051 controller, switches for inputs, an led display and a relay for the motor control, ill get the code as best as i can get in, create the circuit then add the pin numbers after, if this is the right way to go? or would you suggest getting the circuit made then the code?Code:const int MAX = 5 int data [MAX} = {0,1,2,3,4}; // too many people in lift override stop motor and sound alarm void Liftcontrol if (weight = 1) { motor1 = 0; motor2 = 0; alarm =1; } else { void main() int index, temp; Liftcontrol(); { if(elevator = 1) //up elevator { if( data[index] > temp) //if destination is above current floor go to floor { do output to motor1 =1 for (input - current) seconds } else { do ADD input to queue until down elevator } } if (elevator = 0) //down elevator { if( data [index] < temp) //if destination is below current floor go to floor { do output to motor2 =1 for (current - input) seconds } else { do ADD input to queue until up elevator } } }
i want the elevator to start on floor 0 and return to floor 0 when there are no inputs but not sure how to create this loop?