// Ohmmetro V2.2 By Valter & Samuele (Nov. 2007) #include "NXCDefs.h" unsigned long R; // value calculated resistance int nR; // number characters string sR string sR; // string value resistance string s1ohm; // string 1 for value resistance ohms string s2ohm; // string 2 for value resistance ohms string s1kohm; // string 1 for value resistance Kohms string s2kohm; // string 2 for value resistance Kohms /* ---------------------------- SUB sub_ohm --------------------------- */ sub sub_ohm () { sR = NumToStr(R); nR = StrLen (sR); s1ohm = SubStr(sR, 0, nR - 3); s2ohm = SubStr(sR, nR - 3 , 3); if (nR > 3) { sR = StrCat(s1ohm,".",s2ohm); } TextOut(8, 8, " ohm" ,TRUE); TextOut(40, 8, sR); } /* ---------------------------- SUB sub_Kohm -------------------------- */ sub sub_Kohm () { sR = NumToStr(R); nR = StrLen (sR); s1kohm = SubStr(sR, 0, nR - 3); s2kohm = SubStr(sR, nR - 3 , 1); sR = StrCat(s1kohm,",",s2kohm); TextOut(8, 24, "Kohm"); TextOut(40, 24, sR); } /* ---------------------------- START PROGRAM ------------------------- */ task main() { SetSensorType (IN_1, IN_TYPE_SWITCH); // NXT input 1 SetSensorMode (IN_1, IN_MODE_RAW); while(TRUE) { // Calculated: R = (RAW * 10000) / (1023 - RAW) R = (SENSOR_1 * 10000)/(1023 - SENSOR_1); switch (R) { case 0 : TextOut(20, 16, "(Infinite)",TRUE); // display infinite if R = 0 TextOut(6, 8, "Insert Resistor"); break; default : sub_ohm(); // go to sub_ohm if (R >= 1000) { // if value >= 1000 ... sub_Kohm(); // ... go to sub_Kohm } } TextOut(13, 48, "Ohmmetro V2.2"); TextOut(3, 44, "By Valter & Samu"); Wait (400); // pause 0,4 sec (reading data) } } /* ---------------------------- END PROGRAM -------------------------*/