rpm control program

hi.. i am doing a fire extinguisher robot project. and the robot senses fire, stops motor, turns off fan and put off candle fire..its ok.but i cant control the rpm of motors.. sometimes, the robot just smashes the candle without stopping..

i am using 12V DC Motors (4) 300 rpm, h-bridge to control them, arduino atmega 8, cooling fan, dfrobot flame sensor..

i tried using arduino tutorial but still cant crack it...please i need your help.

but i cant control the rpm of motors.

The problem is on line 37. Unless my crystal ball needs polishing again.

cant get what u said

We can't tell what is wrong without seeing your code ....

int m1=13,m3=7; // m1=motor, m3= fan
int fm=A5; // fm= flame sensor
int val;
void setup()

{Serial.begin(9600);
pinMode(fm,INPUT);
pinMode(13, OUTPUT);
pinMode(7, OUTPUT);

}
void loop()
{
val=analogRead(fm); //connected flame sensor to analogpin 5. vcc to +5 volt, gnd.

Serial.println(val ,DEC);
delay(100);
if(val>400) // val>400 represents the presence of fire.
{digitalWrite(13, LOW); // motor gets stopped but not the robot. as i use low torque motor, the robot still moves a bit after stopping.
digitalWrite(7, HIGH); // fan gets on. extinguishes fire.
}
if(val<100) // no significant fire..just the surrounding temperature..
{digitalWrite(13,HIGH);
digitalWrite(7, LOW); // robot keeps running..in search of fire..
}

}

i need to code for rpm control..like from 200-400 (flame sensor readings), it has to be 150 rpm..then from 500 -800, 100 rpm, then from 800, motor has to stop.. like that i want some codes..i tried pwm pin but cant get the output..

val=analogRead(fm); //connected flame sensor to analogpin 5. vcc to +5 volt,  gnd.
     
      Serial.println(val ,DEC);     
      delay(100);

Read the sensor. If it detects flame, keep charging on for a while. Why? GET RID OF THAT DELAY!

You're looking for an "else" :

if ( val > 800 ) 
{
   analogWrite(m1, 0);      // stop
   digitalWrite(m2,HIGH); // fan on
}
if (val < 780)
{  
  digitalWrite(m2, LOW); // fan off 
  if ( val > 400 ) 
  {
      analogWrite(m1, 100); // slow
  }
  else if (val > 200 ) 
  {
      analogWrite(m1, 200); // medium
  }
  else
  {
      analogWrite(m1, 300); // full speed
  }
}

I also added a sample hysteresis : if value is between 780 and 800 there's no change: if fan wan running, it keep running; if its off it stays off until val rises even more.

@michael x

i actually connected motor to digital pin 13. should i connect it to analog? coz you used analogwrite for motor.. or should i just stay in digital.??

Perhaps you read about analogWrite and then think about how you want to control the speed of your motor...

i read it..but cant get the coding right...thats my problem.

I read

On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 through 13. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.

My sample writes to the (digital) pin you defined in m2.

And I don't see your problem, obviously.

as mine is arduino atmega8, i should connect to 11 or 9 th digital pin and then use the analogwrite code.. is that correct?? i thought we should use analogwrite only in the analog pin...

tried that code..couldnt get it right...any help?

tried that code

You misspelled this, and you failed to post the code.

couldnt get it right.

You failed to define what actually happened.

any help?

I can think of some things.

mechatro:
tried that code..couldnt get it right...any help?

Read this before posting a programming question

How to use this forum

Post your code! Inside code tags. The code you "tried". It's no good saying you tried what PaulS said and it isn't right. Show what you tried and describe what wasn't right about it.

ok man..i almost got the solution without making rpm control code.. just by using two relays (+5 volt).. i just posted here if i can come across a solution.. but the solution just sparked into me. :slight_smile: