samedi 31 août 2013

The best way to learn something, is to imagine yourself teaching it


lundi 12 août 2013

Resistor Calculator Android application

This is my first android application, it consists of something that I actually need during making electronic projects where there are so many resistors in the table and I can't just figure out the value of each one of them at a glance.
Resistors used code colors, and each color according to its position has a specific value.


A resistor can have 4, 5 or 6 color bands just like the following picture:

Each color rank have a specific meaning in each Resistor type. For example the third band (from the left) of a 4 band resistor is a multiplier digit, but in the 5 band resistor it is the unity digit.

The previous picture shows a resistor with 4 color bands, organized as follow (from left to right)
Brown, Black, Red, Gold
10*10^ 2 +/- 5%  = 1K ohm with 5% tolerance

For more information about how to calculate resistor color code, please refer to this wonderful wekipedia article:
http://fr.wikipedia.org/wiki/CEI_60757
I actually use this mnemonic in french "ne mange rien ou jeuner voila bien votre grande bétise" to remember the mean of each color in the resistance' but sometimes that could be very confusing, so I thought to create an android app that help with that.
I know that there is many android app doing that, but I did wanted to learn android too :D

The Android application:

In the Main activity, there are 3 buttons for each kind of resistor:
Each button open a new Activity through a simple intent in its OnClickListener() as follow:
btn4BandResistance.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    Intent intent4 = new Intent(MainActivity.this,BandResistor4.class);
    startActivity(intent4);
   }
  });
A new Activity opens, I used The Spinner View to choose from one of the predefined colors, and TextViews to show result:

I used a function that is called in each OnItemSelectedListener() of each spinner to update the results:
spin1Band4.setOnItemSelectedListener(new OnItemSelectedListener() {

   @Override
   public void onItemSelected(AdapterView arg0, View arg1,
     int arg2, long arg3) {
    
    update4BandResistance();
   }

   @Override
   public void onNothingSelected(AdapterView arg0) {
    // TODO Auto-generated method stub
    
   }
  });
the update function link the position of each selected Item to a value already defined in an Array:
 public void update4BandResistance(){
  String unity;
  TotalResistance = (ColorArrayBand[spin1Band4.getSelectedItemPosition()]+ColorArrayBand2[spin2Band4.getSelectedItemPosition()]) * ColorArrayMultiplier[spinMultiply4.getSelectedItemPosition()];
  Tolerance = TotalResistance * ColorArrayTolerance[spin1Tolerance4.getSelectedItemPosition()];
  if(TotalResistance>=1000000){
   TotalResistance/=1000000;
   unity = "M Ohm";
  }
  else if (TotalResistance>=1000){
   TotalResistance /= 1000;
   unity = "K Ohm";
  }
  else {
   unity = "Ohm";
  }
  txtTolerance.setText(String.valueOf(TotalResistance + Tolerance)+"/"+String.valueOf(TotalResistance - Tolerance));
  txtResistance.setText("R = "+String.valueOf(TotalResistance)+unity);  
 }

Finally I added some modification to the result for a more readable value:

The hole project can be found in github:
https://github.com/Mazen21/ResistanceCalculator

I'm looking forward for your suggestions: