Wednesday, February 27, 2013

Arduino uno r3 + lcd sheald + pwm + rpm

I made me a magnetically levitated pulse motor.

The picture and video cloud be found here.

So i thought it would be cool to run this motor with a PWM tecknik using a ARDUINO UNO R3. Before getting this unit running, i had to do some digging and run some trial and error. This unit consist of an LCD SHIELD, a HALL EFFECT SENCOR, a potentiometer, some diode, resistors, capacitors, jumpers and some other small parts.


Hers the code to make it run. Its basically two code's combined into one.
The code is not mine, but i did some alteration to make it work with the LCD to fit the jumpers.

Hopes This helps.

-----------------------------------------------------------------------------------------------------
// include the library code:

#include



// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(8,9,4,5,6,7);

// read the hall effect sensor on pin 2

const int hallPin=2;

const int shiftLed=3;

const unsigned long sampleTime=1000;

const int maxRPM = 50200;

void setup()

{

  pinMode(hallPin,INPUT);

  pinMode(shiftLed,OUTPUT);

   Serial.begin(9600);

  // set up the LCD's number of rows and columns:

  lcd.begin(16, 2);

  // Print a message to the LCD.

  lcd.print("initializing");

  delay(1000);

  lcd.clear();

}

void loop()

{

  delay(100);

  int rpm=getRPM();

  lcd.clear();

  displayRPM(rpm);

//  displayBar(rpm);

}

int getRPM()

{

  // sample for sampleTime in millisecs

  int kount=0;

  boolean kflag=LOW;

  unsigned long currentTime=0;

  unsigned long startTime=millis();

  while (currentTime<=sampleTime)

{

    if (digitalRead(hallPin)==HIGH)

{

    kflag=HIGH;

}

if (digitalRead(hallPin)==LOW && kflag==HIGH)

{

kount++;

kflag=LOW;

}

currentTime=millis()-startTime;

}

int kount2rpm = int(60000./float(sampleTime))*kount;

return kount2rpm;

}

void displayRPM(int rpm)

{

  lcd.clear();

  // set the cursor to column 0, line 1

  lcd.setCursor(0, 0);

  // print the number of seconds since reset:

  lcd.print(rpm,DEC);



  if((rpm)==60)  //change value to own choice

{

  digitalWrite(shiftLed,HIGH);

}

else if((rpm)==120)  //change value to own choice

{

  digitalWrite(shiftLed,HIGH);

}

else if((rpm)==180)  //Change value to own choice

{

  digitalWrite(shiftLed,HIGH);

}

else

{

  digitalWrite(shiftLed,LOW);

}

  lcd.setCursor(7,0);

  lcd.print("NEO RPM");

}

//void displayBar(int rpm)

//{

//  int numOfBars=map(rpm,0,maxRPM,0,15);

//  lcd.setCursor(0,1);

//  if (rpm!=0)

//{

//
//{

// lcd.setCursor(i,1);

// lcd.write(1023);

//}}}