go away spammer

Author Topic: Mobile for my baby son  (Read 1724 times)

0 Members and 1 Guest are viewing this topic.

Offline ConfugereTopic starter

  • Beginner
  • *
  • Posts: 5
  • Helpful? 0
Mobile for my baby son
« on: October 23, 2012, 02:04:40 PM »
Dear Robot guys,
Im working on the speed control for a mobile spinning machine that drives mobiles for my baby son.
The speed control only partly works, when I turn the button the speed only eventually increases or decreases.
Here is my programm so far:
#include <Stepper.h>

const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
                                     // for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, dirA, dirB);           

int stepCount = 0;         // number of steps the motor has taken

int pos = 0;    // variable to store the servo position
int vspeed = 100;
int ispeed = 10;
const int pin_A = A8;
const int pin_B = A9;
unsigned char encoder_A;
unsigned char encoder_B;
unsigned char encoder_A_prev = 0;


void setup() {
  // initialize the serial port:
  pinMode(pwmA, OUTPUT);
  digitalWrite(pwmA, HIGH);
  pinMode(pwmB, OUTPUT);
  digitalWrite(pwmB, HIGH);

  // Turn off the brakes
  pinMode(brakeA, OUTPUT);
  digitalWrite(brakeA, LOW);
  pinMode(brakeB, OUTPUT);
  digitalWrite(brakeB, LOW);

  // Log some shit
  Serial.begin(9600);
  pinMode(pin_A, INPUT);
  pinMode(pin_B, INPUT);
}

void loop()
{
  for(pos = 0; pos < 500; pos += 1)  // goes from 0 degrees to 180 degrees
  {
    // step one step:
    myStepper.step(1);
    stepCount++;
    //Serial.print("Position:" );
    //Serial.println(pos);
    //Serial.print("Schritte:" );
    //Serial.println(stepCount);
    encoder_A = digitalRead(pin_A);
    encoder_B = digitalRead(pin_B);
    if((!encoder_A) && (encoder_A_prev)){
      // A has gone from high to low
      if(encoder_B) {
        // B is high so clockwise
        // increase the brightness, dont go over 255
        if(vspeed + ispeed <= 255) vspeed += ispeed;               
      }   
      else {
        // B is low so counter-clockwise     
        // decrease the brightness, dont go below 0
        if(vspeed - ispeed >= 0) vspeed -= ispeed;               
      }
      encoder_A_prev = encoder_A;
    delay(vspeed);
    Serial.print(vspeed);
    }
  }
  for(pos = 500; pos >= 1; pos -= 1)  // goes from 0 degrees to 180 degrees
  {
    // step one step:
    myStepper.step(-1);
    stepCount++;
    //Serial.print("Position:" );
    //Serial.println(pos);
    //Serial.print("Schritte:" );
    //Serial.println(stepCount);
    encoder_A = digitalRead(pin_A);
    encoder_B = digitalRead(pin_B);
    if((!encoder_A) && (encoder_A_prev)){
      // A has gone from high to low
      if(encoder_B) {
        // B is high so clockwise
        // increase the brightness, dont go over 255
        if(vspeed + ispeed <= 255) vspeed += ispeed;               
      }   
      else {
        // B is low so counter-clockwise     
        // decrease the brightness, dont go below 0
        if(vspeed - ispeed >= 0) vspeed -= ispeed;               
      }
    }
    encoder_A_prev = encoder_A; 
    delay(vspeed);
    Serial.println(vspeed);
 }
}

I hope you have an idea why its not working.
Greetings con

Offline Mr. Ninja

  • Jr. Member
  • **
  • Posts: 35
  • Helpful? 0
Re: Mobile for my baby son
« Reply #1 on: October 27, 2012, 11:41:25 AM »
First off, are you using the Arduino? Or are you using some other board?

If you aren't using the Arduino, that code will not work, as the Arduino has different functions to write to the pins than you would do with regular C.

Offline ConfugereTopic starter

  • Beginner
  • *
  • Posts: 5
  • Helpful? 0
Re: Mobile for my baby son
« Reply #2 on: October 29, 2012, 07:46:57 AM »
Forgot to mention that Im using the arduino board equipped with a motor shield.

 


Get Your Ad Here