View Single Post
Old 10-03-2013, 02:57 PM   #2 (permalink)
Quezacotl
Scandinavian creature
 
Join Date: Jun 2012
Location: Finland
Posts: 146

Golf ball - '94 Volkswagen Golf III
90 day: 28.46 mpg (US)
Thanks: 4
Thanked 27 Times in 22 Posts
And here's the code, pin numbering might be other than in schematic, but it's easy to change.

Code:
/*
Cruise control by Marko Hämäläinen. mthtek.net
Compiled with Adruino IDE 1.0.4, with Atmega328

So, this is actually an constant throttle control,
but will be developed into constant speed control as soon as i learn to code that. And i will get rid of the TimerOne-library.

When you press either + or - button, the stepper motor nudges one step, locks to it's position, and fan starts blowing.
When you press the reset-button, it releases the stepper motor and fan stops blowing.
Reset-button should be wired that either clutch, brake, acceleration or a switch can trigger it. For safety!
Plus and minus-buttons can be wired wherever user wants them to be.

Stepper motor-driver used in this is L293D chip, using pwm on enable pins,
but because of excess heating(needs a fan and/or big sink) still while using pwm, i will replace it with FETs at some point.

Included library, TimerOne: http://playground.arduino.cc/code/timer1

A4 = pwm potentiometer
A5 = step speed potentiometer
3 = fan control (optional)
5 = spare for speedometer (not implemented yet)
6 = release motor
7 = step counter-clockwise
8 = step clockwise
10 = motor enable/disable/pwm (must be pwm pin)
(9,13),(11,12) = stepper
*/

#include <TimerOne.h>
#include <Stepper.h>
// set the button pins working
int pot = A4;
int pwmPot = A5;
byte fan = 3; // optional fan for cooling when the stepper is on
byte plusButton = 8; // step clockwise
byte minusButton = 7; // step counter-clockwise
byte resetPin = 6; // release motor
byte motorState = 10; // L293D enable pin
byte plusState = HIGH; // set plusbutton HIGH
byte minusState = HIGH; // set minusbutton HIGH
byte resetState = HIGH; // set resetButton HIGH
int potState = 0;
int pwmPotState = 0;
// set the stepper function working
int stepsPerRevolution = 200;  // 1,3 degrees x 200 = 360 degrees
Stepper myStepper(stepsPerRevolution, 9,13,11,12); // digital pins 6,7,8,9
// variables
int stepCount = 0; // count the steps

void setup() {

  Timer1.initialize(25);         // 25 = 40kHz
  Timer1.pwm(motorState, 512);                // “pin”, “0-1024 = 0-100% duty”

  pinMode(plusButton, INPUT); // initialize buttons for inputs
  pinMode(minusButton, INPUT);
  pinMode(resetPin, INPUT);
  pinMode(pot, INPUT);
  pinMode(pwmPot, INPUT);
  pinMode(motorState, OUTPUT); // initialize outputs
  pinMode(fan, OUTPUT);
}

void loop(){
  // Read the state of the buttons and pots in beginning of every loop
  plusState = digitalRead(plusButton);
  minusState = digitalRead(minusButton);
  resetState = digitalRead(resetPin);
  potState = analogRead(pot);
  pwmPotState = analogRead(pwmPot);
 
  if (plusState == LOW && stepCount <= 700) {
    Timer1.pwm(motorState, pwmPotState);
    digitalWrite(fan, HIGH);
    myStepper.step(1);
    stepCount++;
    delay(potState);
  }
  if (minusState == LOW && stepCount >= -700) { 
    Timer1.pwm(motorState, pwmPotState);
    digitalWrite(fan, HIGH);
    myStepper.step(-1);
    stepCount--;
    delay(potState);
  }
  if (resetState == LOW) { // touch clutch, acc or brake = stepper off
    digitalWrite(motorState, LOW);
    digitalWrite(fan, LOW);
    stepCount = 0; // reset the position
  }
}
__________________

Brrrmm
Now selling preassembled MPGuino's!
http://www.mthtek.net/mpguino/
  Reply With Quote