variable motor speed through time

hello..im workin on my final year university project..now i have a problem to control the speed of the 12v motor..i need to control the speed through time..means the speed is vary over time..can someone provide a guide for the code.

Take a look at the [u]LED Fading Example[/u]. Fading an LED with PWM is similar to speed-controlling a DC motor.

hello..im workin on my final year university project..now i have a problem to control the speed of the 12v motor.

Are you an engineering student, or is your degree in something like art?

If you are an engineering student, the LED fading example should be enough to get you started, and you should be able to take it from there. If you don't know much about electronics, programming, or motors, you may going to need a bit more help...

If you are not an engineering student, do you know anything about programming or electronics? Can you tell us a bit more about the motor, what it's doing, and what's supposed to determine the speed? Does the speed need to be precise?

There are example programs for most common activities within the Arduino IDE.

You should also look at the demo several things at a time as it shows how to manage time with the millis() function.

If you are new to the Arduino the Thread planning and implementing a program may have some useful stuff.

...R

DVDdoug:
Take a look at the [u]LED Fading Example[/u]. Fading an LED with PWM is similar to speed-controlling a DC motor.

Are you an engineering student, or is your degree in something like art?

If you are an engineering student, the LED fading example should be enough to get you started, and you should be able to take it from there. If you don't know much about electronics, programming, or motors, you may going to need a bit more help...

If you are not an engineering student, do you know anything about programming or electronics? Can you tell us a bit more about the motor, what it's doing, and what's supposed to determine the speed? Does the speed need to be precise?

yes..im a mechanical degree student..my project is to create an automation system for "gula melaka" (some kind of a mixture)

the system need to control the temperature of the mixture at the same time show the value of the temperature to the lcd panel and also control the motor speed to blend the mixture(which vary over time to time)..

the speed doesnt need to be precise..i just need to show that the motor can be automated using arduino..

i already done the part where it control the temperature and show the value to the lcd..right now my prof ask to add the motor part..did i need to change the whole code or can i just add few line for the motor control..

thanks in advance!

cep3k:
did i need to change the whole code or can i just add few line for the motor control..

Just change lines 23 and 57.

Oh, sorry, you haven't posted your code yet.

...R

PS - Just remind me, which of us is going to get the degree?

Robin2:
Oh, sorry, you haven't posted your code yet.

#include <LiquidCrystal.h>
#include <MAX6675.h>

#define swStart 4
#define swStop  2
#define stbyMode A4
#define onMode A5
#define heater 3
#define CS 11
#define SO 12
#define SCK 13

int units = 1;
float tempRead = 0.0;

MAX6675 temp(CS,SO,SCK,units);

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

uint8_t degree[8] = {140,146,146,140,128,128,128,128};

int ctrlStart;
int ctrlStop;
int onState;
long previousMillis;
long currentMillis;
long timeSet=3000000;
long timeCount;
int tempSet=250;
int processStart = LOW;

void setup()
{
  
  Serial.begin(9600);
  lcd.begin(16,2);
  
  pinMode(swStart, INPUT);
  pinMode(swStop,INPUT);
  pinMode(onMode,OUTPUT);
  pinMode(stbyMode,OUTPUT);
  pinMode(heater,OUTPUT);
  
  lcd.createChar(0,degree);
  lcd.setCursor(0,0);
  lcd.print("Gula Melaka R11");
  delay(2000);
  lcd.clear();
  lcd.print("Heater System");
  
}

void loop()

  {
  while (Serial.available()> 0)
  {
    int tempVal = Serial.parseInt();
    int timeVal = Serial.parseInt();
    if (Serial.read() == '\r')
    {
    tempSet = constrain(tempVal, 50, 350);
    timeSet = constrain(timeVal, 10, 99);
    Serial.print("TempSet degC: ");
    Serial.println(tempSet);
    Serial.print("TimeSet Mins: ");
    Serial.println(timeSet);
    timeSet = timeSet * 60000;
    }
  }
  
   ctrlStop = digitalRead(swStop);
   ctrlStart = digitalRead(swStart);
   if(ctrlStop == LOW && ctrlStart == HIGH)
  {
    digitalWrite(onMode, LOW);
    digitalWrite(stbyMode, HIGH);
    lcd.setCursor(0,1);
    lcd.print("Stop ");
    onState = LOW;
    digitalWrite(heater, LOW);
  }
  else if (ctrlStop == HIGH && ctrlStart == LOW)
  {
    digitalWrite(onMode,HIGH);
    digitalWrite(stbyMode,LOW);
    lcd.setCursor(0,0);
    lcd.clear();
    lcd.print("Set:");
    lcd.print(tempSet);
    lcd.write((byte)0);
    lcd.print("C");
    lcd.setCursor(0,1);
    lcd.print("Start");
    onState = HIGH;
    digitalWrite(heater, HIGH);
    processStart = HIGH;
  }
  
  if(onState == HIGH)
  {
    tempRead = temp.read_temp(); 
    if(tempRead < 0)
   {
    Serial.print("Thermocouple Error");
    Serial.println(tempRead);
    lcd.setCursor(7,1);
    lcd.print("T:");
    lcd.print("Error");
    digitalWrite(heater,LOW);
   }
   else
   {
    Serial.print("Current Temperature");
    Serial.println(tempRead);
    lcd.setCursor(7,1);
    lcd.print("T:");
    lcd.print(tempRead);
    lcd.write((byte)0);
    lcd.print("C");
    if(tempSet < tempRead && processStart == HIGH)
    
   {
      previousMillis = millis();
      processStart = LOW;
   }
   
   else if(tempSet < tempRead)
 {  
     currentMillis = millis();
     timeCount = (currentMillis - previousMillis)/60000;
     Serial.println(timeCount);
     lcd.setCursor(10,0);
     lcd.print("M:");
     lcd.print(timeCount);
     if(currentMillis - previousMillis > timeSet)
     
  {
  onState=LOW;
   }
   
   else
   {
    digitalWrite(heater,LOW);
    digitalWrite(onMode,LOW);
   }
 }
 else
 {
   digitalWrite(onMode, HIGH);
   digitalWrite(heater,LOW);
 }
   }
   
   delay(300);
  }
  else if(onState == LOW)
  {
    digitalWrite(heater,LOW);
    digitalWrite(onMode,LOW);
    digitalWrite(stbyMode,HIGH);
    lcd.setCursor(0,0);
    lcd.print("Heater System   ");
    lcd.setCursor(0,1);
    lcd.print("Stop");
  }
  
}

I'd be surprised if this is working properly

** while (Serial.available()> 0)**
** {**
** int tempVal = Serial.parseInt();**
** int timeVal = Serial.parseInt();**

Serial.available()> 0 will only tell you that there's SOMETHING available. It could be a single digit or a whole string. Yet your code immediately assumes there's two values.
Furthermore.
int tempVal = Serial.parseInt();
will read from the serial until a non numeric character is found. But if it was just the first digit of a multiple digit value, it's going to remove that before the next characters arrive.
Furthermore
int timeVal = Serial.parseInt();
Shouldn't be able to find any numeric character as the previous statement would have already taken them.

Sorry, just sayin'

In addition to what @KenF has said ...

You haven't given us any description of how the code is supposed to work - what should each part do?

I don't see any code that is trying to control the speed of a motor.

If it was my project I would strip out all the LCD code and leave that for last - after everything else works. That way your code will be shorter and easier to follow.

And I would also divide it into several short functions that each deal with a specific issue (as in the links I posted earlier)

...R