Created
March 5, 2015 17:32
-
-
Save johnholbrook/d198a04dacc320b853f4 to your computer and use it in GitHub Desktop.
A basic four-function calculator written in ROBOTC for RCX. Video at http://youtu.be/Wa_-ehRMjs0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma config(Sensor, S1, select, sensorTouch) | |
#pragma config(Sensor, S2, dial, sensorRotation) | |
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// | |
task main() | |
{ | |
SensorValue[dial] = 0; //set first number | |
bool choosing; | |
choosing = true; | |
int num1; | |
while (choosing == true) | |
{ | |
SetUserDisplay(SensorValue[dial]); | |
if (SensorValue[select] == 1) | |
{ | |
num1 = SensorValue[dial]; | |
choosing = false; | |
} | |
} | |
PlayTone(220,50); | |
wait10Msec(50); | |
int quarter; //select mathematical operation | |
int operation; | |
SensorValue[dial] = 0; | |
choosing = true; | |
while (choosing == true) | |
{ | |
quarter = (SensorValue[dial]/4)%4; | |
if (quarter < 0){quarter*=-1;} | |
if (quarter == 0) | |
{ | |
SetUserDisplay(1); //add | |
} | |
if (quarter == 1) | |
{ | |
SetUserDisplay(2); //subtract | |
} | |
if (quarter == 2) | |
{ | |
SetUserDisplay(3); //multiply | |
} | |
if (quarter == 3) | |
{ | |
SetUserDisplay(4); //divide | |
} | |
if (SensorValue[select] == 1) | |
{ | |
operation = quarter+1; | |
choosing = false; | |
} | |
} | |
PlayTone(220,50); | |
wait10Msec(50); | |
SensorValue[dial] = 0; //set second number | |
int num2; | |
choosing = true; | |
while (choosing == true) | |
{ | |
SetUserDisplay(SensorValue[dial]); | |
if (SensorValue[select] == 1) | |
{ | |
num2 = SensorValue[dial]; | |
choosing = false; | |
} | |
} | |
PlayTone(220,50); | |
int result; //calculate result | |
if (operation == 1) //add | |
{ | |
result = num1+num2; | |
} | |
if (operation == 2) //subtract | |
{ | |
result = num1-num2; | |
} | |
if (operation == 3) //multiply | |
{ | |
result = num1*num2; | |
} | |
if (operation == 4) | |
{ | |
result = num1/num2; | |
} | |
SetUserDisplay(result); | |
choosing = true; //waits for button to be pressed before starting again | |
wait1Msec(10000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment