Loading...
  Show Posts
Pages: 1 ... 34 35 [36] 37 38 ... 50
526  Using Arduino / General Electronics / Re: best way to get components from pcb on: August 11, 2012, 04:16:47 pm
Also try with a solder sucker. It sucks the solder out of the hole.
527  Using Arduino / Programming Questions / Re: Need help with analog out to led's SECOND TRY on: August 11, 2012, 02:27:00 am
Not picky, precise. If the manual says to spell it a particular way, you better stick with that ...
528  Using Arduino / Programming Questions / Re: Need help with analog out to led's SECOND TRY on: August 11, 2012, 01:42:44 am
The programming language is case sensitive and you need to use lower case letters for the keywords. The rest is your choice.
529  Using Arduino / General Electronics / Re: LM298 question on: August 09, 2012, 05:37:43 am
In case it helps, here is some code I wrote to test a board that I made simlar to yours. It is controlled by 4 buttons for Forward, Reverse, Stop and Emergency Stop (active braking). I also had an interrupt driven rev counter - just don't connect to these inputs and it won't matter at all.

Code:
#define  STATE_DEBUG  1
#define  SPEED_DEBUG  0

#define  IN1  5 // input1 on L298
#define  IN2  6 // input2 on L298
#define  ENA  7 // enable A on L298

#define  BUT_FWD  11  // forward
#define  BUT_REV  10  // reverse
#define  BUT_STOP  9  // stop
#define  BUT_ESTOP 8  // e-stop

// RPM counter and ISR
#define  PULSE    2            // pulse input from feeback device
#define  PULSE_PER_REV  10     // pulses counted per revolution - this depends on the feeback device
#define  RPM_CALC_INTERVAL  500  // milliseconds 
volatile long pulseCount = 0;  // pulse counter
unsigned int  motorRPM;        // calculated RPM

// Defines for command buttons
#define  CMD_ESTOP 0
#define  CMD_STOP  1
#define  CMD_FWD   2
#define  CMD_REV   3

volatile uint8_t Command = CMD_ESTOP;

// Motor constants
#define  MIN_POWER  70      // PWM setting for minimum movement of motor
#define  MAX_POWER  255     // PWM setting for maximum power to motor

// The current stater of the motor
#define  MOTOR_BRAKE         0
#define  MOTOR_STOP          1
#define  MOTOR_RUN           2
#define  MOTOR_PRE_RAMP_UP   3
#define  MOTOR_RAMP_UP       4
#define  MOTOR_PRE_RAMP_DOWN 5
#define  MOTOR_RAMP_DOWN     6

#define  DIR_UNDEF  0
#define  DIR_FWD    1
#define  DIR_REV    2

#define  RAMP_UP_TIME    1000  // ms
#define  RAMP_DOWN_TIME  1000  // ms

uint8_t  motorState = MOTOR_BRAKE;     // current state for the motor
uint8_t  motorDirection = DIR_UNDEF;   // current direction for the motor
uint16_t motorSpeedSP = MAX_POWER;     // motor speed setpoint (MIN_POWER, MAX_POWER)
uint8_t  motorSpeedCV;                 // motor speed current value
unsigned long  timerRamp = 0;          // timer to keep track of ramping time elapsed

#if  STATE_DEBUG
#define  StateTransition(S, N)  { Serial.print(State2Str(S)); Serial.print(" -> "); S = (N); Serial.println(State2Str(S)); }
#else
#define  StateTransition(S, N)  S = (N);
#endif

const char* State2Str(uint8_t S)
{
  switch (S)
  {
    case MOTOR_BRAKE:          return("MOTOR_BRAKE");
    case MOTOR_STOP:           return("MOTOR_STOP");
    case MOTOR_RUN:            return("MOTOR_RUN");
    case MOTOR_PRE_RAMP_UP:    return("MOTOR_PRE_RAMP_UP");
    case MOTOR_RAMP_UP:        return("MOTOR_RAMP_UP");
    case MOTOR_PRE_RAMP_DOWN:  return("MOTOR_PRE_RAMP_DOWN");
    case MOTOR_RAMP_DOWN:      return("MOTOR_RAMP_DOWN");
  }
  return("???");
}

void setup ()
{
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(ENA,OUTPUT);

  pinMode(BUT_REV, INPUT);     
  pinMode(BUT_FWD, INPUT);     
  pinMode(BUT_STOP, INPUT);     
  pinMode(BUT_ESTOP, INPUT);     

  pinMode(PULSE, INPUT);

#if (STATE_DEBUG || SPEED_DEBUG)
  Serial.begin(57600);
  Serial.println("[L298 Motor Control Tester]");
#endif

  // finally connect thre interrupt
  attachInterrupt((PULSE == 2) ? 0 : 1, irqPulseCounter, RISING);
}

uint8_t ReadCommand(uint8_t cmdDefault)
{
  // check change of state
  if      (digitalRead(BUT_ESTOP)) return(CMD_ESTOP);
  else if (digitalRead(BUT_STOP))  return(CMD_STOP);
  else if (digitalRead(BUT_FWD))   return(CMD_FWD);
  else if (digitalRead(BUT_REV))   return(CMD_REV);

  return(cmdDefault);
}

// IRQ and RPM routines
void irqPulseCounter()
{
  pulseCount++;
}

void CalculateRPM()
{
  static uint8_t  lastState = MOTOR_STOP;
  static unsigned long timerZero;       // baseline of millis() function for current calculations
 
  if (motorState == MOTOR_RUN)    // is currently running
  {
    if (lastState == MOTOR_RUN)   // and was running on the last call
    {
      if (millis()-timerZero < RPM_CALC_INTERVAL) return;  // only calculate every CALC_INTERVAL ms 
      motorRPM = 1000 * (pulseCount / PULSE_PER_REV) / (millis() - timerZero);
    }

    // either has just started running or we need to reset for the calculation
    pulseCount = 0;
    timerZero = millis();   
  }
  else                            // is not currently running
  {
    if (lastState == MOTOR_RUN)   // and was running on the last call
      motorRPM = 0;
  }   
 
  lastState = motorState;
}

void SetSpeed(uint8_t direction, uint8_t speed)
{
    if (direction == DIR_FWD)
    {
      digitalWrite(IN1, LOW);
      analogWrite(IN2, speed);
      digitalWrite(ENA, HIGH);
    }
    else
    {
      analogWrite(IN1, speed);
      digitalWrite(IN2, LOW);
      digitalWrite(ENA, HIGH);
    }
}

void loop()
{
#if  SPEED_DEBUG
  Serial.print("R ");
  Serial.print(motorRPM);
  Serial.print(": S ");
  if (motorDirection != DIR_UNDEF)
    Serial.print(motorDirection == DIR_FWD ? "+" : "-");
  Serial.print(motorSpeedCV);
  Serial.print("\n");
#endif

  Command = ReadCommand(Command);
 
  if ((Command == CMD_ESTOP) && (motorState != MOTOR_STOP))
  {
    StateTransition(motorState, MOTOR_BRAKE);
  }
 
  switch(motorState)
  {
  case MOTOR_BRAKE:
    digitalWrite(IN1,HIGH);
    digitalWrite(IN2,HIGH);
    digitalWrite(ENA,HIGH);
    motorDirection = DIR_UNDEF;
    motorSpeedCV = 0;
    StateTransition(motorState, MOTOR_STOP);
    break;
   
  case MOTOR_STOP:
    if ((Command == CMD_FWD) || (Command == CMD_REV))
    {
      motorDirection = (Command == CMD_FWD) ? DIR_FWD : DIR_REV;
      StateTransition(motorState, MOTOR_PRE_RAMP_UP);
    }
    break;
   
  case MOTOR_PRE_RAMP_UP:
    motorSpeedCV = MIN_POWER;
    timerRamp = millis();
    StateTransition(motorState, MOTOR_RAMP_UP);
    break;
   
  case MOTOR_RAMP_UP:
    if (millis()-timerRamp < RAMP_UP_TIME)
    {
      motorSpeedCV = MIN_POWER + (((motorSpeedSP-MIN_POWER) * (millis()-timerRamp)) / RAMP_UP_TIME);
    }
    else
    {
      motorSpeedCV = motorSpeedSP;
      StateTransition(motorState, MOTOR_RUN);
    }
    SetSpeed(motorDirection, motorSpeedCV);
    break;
   
  case MOTOR_RUN:
    if (Command == CMD_STOP)
    {
      StateTransition(motorState, MOTOR_PRE_RAMP_DOWN);
    }
    break;

  case MOTOR_PRE_RAMP_DOWN:
    timerRamp = millis();
    StateTransition(motorState, MOTOR_RAMP_DOWN);
    break;
   
  case MOTOR_RAMP_DOWN:
    if (millis()-timerRamp < RAMP_DOWN_TIME)
    {
      motorSpeedCV = motorSpeedSP - (((motorSpeedSP-MIN_POWER) * (millis()-timerRamp)) / RAMP_DOWN_TIME);
    }
    else
    {
      StateTransition(motorState, MOTOR_BRAKE);
    }
    SetSpeed(motorDirection, motorSpeedCV);
    break;
  }

  CalculateRPM();
}

530  Using Arduino / General Electronics / Re: interfacing 10 turn precision potentiometers to analog In on: August 06, 2012, 02:54:30 am
Are you wired with Pot pin 1 and 3 to the power supply and pin 2 (the wiper) to the analog input?
531  Using Arduino / Programming Questions / Re: Very simple question about array [invalid types 'int[int]' for array subscript] on: August 04, 2012, 07:25:53 am
If tab is a global variable you do not need to pass it as a parameter. Your local definition is an int and is overriding the global declaration, which is the array.
532  Community / Bar Sport / Re: Ardunio C++ SUCKS!!!! on: August 02, 2012, 07:50:43 am
Have you heard of Google? Try looking for C++ tutorial and educate before ranting.

Arduino libraries may not be the bst documented as it depends entirely on mostly amateur programmers to do the right thing, so learn C++ and then read the code. Better still, as you learn, document it for other people so that others don't feel the same frustration.
533  Using Arduino / General Electronics / Re: LM298 question on: August 02, 2012, 07:01:35 am
This depends on what features you want to implement. Google lm298 circuit and lots comes up.
534  Using Arduino / Programming Questions / Re: algorithm to detect a blinking LED on: August 02, 2012, 06:29:25 am
I am describing an algorithm, not an implementation. In principle no matter what the speed you could use the same method.  Agree in practice it suits the way the OP described requirements rather than high speed.
535  Using Arduino / Programming Questions / Re: algorithm to detect a blinking LED on: August 02, 2012, 06:21:06 am
If the blink is regular, no matter what the speed, then you can time between The detection of low to high transition an see if there is a similar interval. If the interval is significantly different (defined by some dead band you set) then reset the detection and start again.
536  Using Arduino / Programming Questions / Re: Question about Blink Program Using Switch Case on: August 02, 2012, 03:04:38 am
Switch statements require an exact match and act like the '==' operator. They are good if you can guarantee (or conspire to create) this situation.

In your case, you probably want to trigger if the time is >2000, as it is highly unlikelythat the millis() value will exactly be 2000 unless you are lucky.

Also, you cannot create complex boolean statements in the switch statement, so 'if' will live for a long time ...
537  Using Arduino / Programming Questions / Re: Help with converting integer to unsigned char* on: August 01, 2012, 05:33:25 am
If this is the function you are using you cannot just return ubuffer and use it as it is only valid in the function. Outside of the function the value is not defined and will probably be corrupted. You need to add the word static in front of the variable declaration for this to work.
538  Using Arduino / Programming Questions / Re: Help with converting integer to unsigned char* on: August 01, 2012, 03:52:14 am
I really also think that OP needs to clarify if he needs to convert the numbers to a string or if the value is just passed in what is effectively a byte buffer.
539  Using Arduino / Programming Questions / Re: Help with converting integer to unsigned char* on: August 01, 2012, 03:44:57 am
The easiest way is to cast the value when you call the function, provided you know the library is simply expecting a buffer with the value and not actually chars. Cast by putting the desired type in front of the variable name.

Code:
myFunction((char *) myVarName)
540  Using Arduino / Programming Questions / Re: const + struct + array on: July 29, 2012, 05:38:42 am
I think the typedef is the issue. You need to declare it in a header file for it to work. Seemed to fix a similar issue I had in the past.
Pages: 1 ... 34 35 [36] 37 38 ... 50