Go Down

Topic: Float to Int conversion problem (Read 3669 times) previous topic - next topic

tim1986

I am trying to use a slider potentiometer to control the master volume on my PC. I have the AutoIt program written and it needs the volume in a percentage. I am trying to convert the value from the slider pot to a percentage using the formula of (value/maxvalue)*100. I am doing the calculation in a float and then converting it to a int and sending it over serial. Please help me figure this out.
-Tim

The code marked
////////////
///HERE///
////////////
is the calculation.


Here is my code:

Code: [Select]
/*
*  SerialVolumeSlider
*  
*  Reads an analog input from the input pin and sends the value
*  as a percentage followed by a line break over the serial port.
*
*/

// variables for input pin and control LED
 int analogInput = 4;
 int LEDpin = 13;
 int incomingByte = 0;      // for incoming serial data
 int maxValue = 1023;
 
// variable to store the value
 float fvalue = 0.00;
 int value = 0;
 
// a threshold to decide when the LED turns on
 int threshold = 512;
 
void setup(){

// declaration of pin modes
 pinMode(analogInput, INPUT);
 pinMode(LEDpin, OUTPUT);
 
// begin sending over serial port
 beginSerial(9600);
 Serial.println("Ready.");
}

void loop(){
// read the value on analog input
 value = analogRead(analogInput);

// if value greater than threshold turn on LED
if (value < threshold) digitalWrite(LEDpin, HIGH); else digitalWrite(LEDpin, LOW);

////////////
///HERE///
////////////
fvalue = (value/maxValue)*100;
value = (int)(fvalue);

if (getping() == 1) {
 Serial.println(value);
 delay(10);
}

//  printInteger(value);

int getping(){
 incomingByte = Serial.read();
 if (incomingByte == 80) {
   //Serial.println(incomingByte);
   return 1;
 }
 else {
   return 0;
 }
}

tim1986

Now when I plug in my Arduino, without doing anything to it, using the Serial Monitor in Arduino it gives me this:

Now when I plug in my Arduino, without doing anything to it, using the Serial Monitor in Arduino it gives me this:

Code: [Select]
Ready.

9188908891908289968989908184899090899189899190908491909090898398919091878191889190908090889090908590899088919090909190898591919188908990898988889089889191878990899090838994929089899488909092839191908992868989909091839890889091908890908992918991889190909489909089898890909091908889908890939092898984929792899184919089909189869189909189968888889090901009090828789898990929089898889869189909188899990898890829094899293928491909091899492929090889790899186889690918988828790898989898991898989849192888993868990928991959290908982909689929190838992909192919090899291889491898592888890899080859290898889909189909089939289919187968989898883898890899089898992908986918989889389889090898185908890928890928988898288958990908890919090918996939089908989909288908994909090908990909089908888899289899088889088919096918889908489898993919396908992928789918990909291899190829189918990918990929092909296899185919789888888898897909188849390899089849190899089869490899183899590909191929090889081889690909090889088929290879292889090888991889183909690918886918989899290929090899091908890908989989089898890938892909085908990908886888990888890969090908681908989929091888889908886908992939090978990898189998887919090909088919093979090909098909090879093909089919089909190868894889190918392898889869090898890929288889089848894909089888289888991818988919190909288908989849088898891899690918889899291889191908788889092889691899192919188908891868990909090848989899191918988908983899090928990919289888890908992898882899490899180849090899089909090899092909988918989899090908982899090889090899090909188929089899081928991909192979091889190899089929292988891918789919090898189929088898989908990899092909190928993918888909097908990878890889190918998928991909089929289909090889089918592909190919297928988838990888989919090908991928589908990899691908890809098908990899089908991909198898889908990908984889393899190909090909288889589898891828989908990901009090898290909092918191909088909096909091918288899290899095909091929192898890828998909189899088919089899288889089859090918889859690919091909289909086899489909090818989908988929090908991928990918988889689919089829190909091919389918992879191909192908888909080928989899084888989888983889190908990889290918882899389919189899090918985929090899284889090908089998988909189889191889091969289909186909188908190958989899084889292899093909089908189999091909196889192898389898889909091908892879193888991908993899388889296899090918990888891909098899089919289898894878894899089899191909089918991898892899389898991849292899190879991898889839091919191919090928893869090928890928888898982899788899088909288899090919889909089879189909192959290889089858890909091948889889082899689889292978990909089919588909089979188908783909090929091909090909089909291898891908892928992899089919188899088929091909190909088908889898589899290898695929292908494908990918990909090899089919089889092908888908895909091908989899190908999909088888390899191929390909089808890909091909189909190881019090918888929089889289929089918690909291919090909088938990899290899096889188899087919090909096899089898490899291908896908891908590899190909088899089849296919089929888889190808592889189908990888990938899909289858289909091909888899091898988909390929790909190808990908989898990889092889091919083909689888888899191909190909189919288899390898990919091908888889088919186909088908989938889919186979288889083898990908992908989919183929090899190908989898090908888919082899190898990988991909090929090928288988890898995898891919093989189918886919090899089928990889089899089909291918889908288929089908892908889898989889088929199909089869197888889909293928991898993889190919090899090829388889091879988908988929090899191869790888983859288908989859190899188859390909190869391909181869190919090899090908982899790918890899088929080899488899090848990929088939091888881848991899089909089918981919088909091909089898890909090909092899089908690959091888699909190919190909090918899909090908982889090909290909090908989918989879193899089898092899089918589909091949292899089928789899089888390899090909191899293928589909089918290909092829097908989898989889089848990918889908491909189829292889189908590929090919391899190899192898989909790918891899288909091889690889087908990899090899691888988878989909082929789909084898892888989889090918881909091909088889189898990919190898885939090909


and it just keeps going like that till I stop the monitor. Did I fry my Arduino??? Help!

mellis

One problem is that you're missing a closing curly brace } on your loop() function.  I'm not sure why this doesn't give an error message, but it probably is causing problems.

Another problem you'll have is that the calculation value/maxValue will be done using integer math because both arguments are integers.  That means that you'll get an integer quotient, i.e. anything from 0 to 1022 divided by 1023 is 0; and for example, 11 / 4 is 2.  Try making maxValue a float, perhaps, otherwise you can cast one of them to a float before doing the division, e.g. ((float) value) / maxValue.


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy