Show Posts
Pages: [1]
1  Using Arduino / Project Guidance / Re: RC receiver on Mega2560 on: September 23, 2013, 04:45:47 am
Hi. Thanks for the mind kick. Micros() saved the thing. It seams TCNT1 has other functions on Mega2560 rather then Timer...
2  Using Arduino / Project Guidance / RC receiver on Mega2560 on: September 22, 2013, 03:38:10 pm
Hi. I use following code, but all I get are random numbers. They are ascending then descending... Any idea where the error is? The 5V and GND are firmly connected.
I get numbers wobbling around zero... It does not matter how I turn sliders on RC transmitter...

Code:
#include <inttypes.h>
#include <PinChangeInt.h>

#define RC_THROTTLE A11
#define RC_STEERING A12
#define RC_MOWER A13

// These bit flags are set in bUpdateFlagsShared to indicate which channels have new signals
#define THROTTLE_FLAG 1
#define STEERING_FLAG 2
#define MOWER_FLAG 4
volatile byte bUpdateFlagsShared; // holds the update flags defined above
volatile unsigned int unThrottleInShared;
volatile unsigned int unSteeringInShared;
volatile unsigned int unMowerInShared;

uint16_t unThrottleInStart;
uint16_t unSteeringInStart;
uint16_t unMowerInStart;
uint16_t unLastMowerIn = 0;
uint32_t ulVariance = 0;
uint32_t ulGetNextSampleMillis = 0;
uint16_t unMaxDifference = 0;

void setup(void) {
  analogReference(INTERNAL2V56);
  Serial.begin(57600);
  Serial.println(F("Welcome"));
  //Set pin directions
  PCintPort::attachInterrupt(RC_THROTTLE, calcThrottle, CHANGE);
  PCintPort::attachInterrupt(RC_STEERING, calcSteering, CHANGE);
  PCintPort::attachInterrupt(RC_MOWER, calcMower, CHANGE);
 
  Serial.println(F("ENDING SETUP"));
  Serial.flush();
  interrupts();
}

word arr[1000], avrg, minn, maxx;
word i = 0;

void loop(void) {
      static uint16_t unThrottleIn;
      static uint16_t unSteeringIn;
      static uint16_t unMowerIn;
      static uint8_t bUpdateFlags; // local copy of update flags
   
      // check shared update flags to see if any channels have a new signal
      if (bUpdateFlagsShared) {
        noInterrupts(); // turn interrupts off quickly while we take local copies of the shared variables
        bUpdateFlags = bUpdateFlagsShared; // take a local copy of which channels were updated in case we need to use this in the rest of loop
     
        // in the current code, the shared values are always populated so we could copy them without testing the flags
        // however in the future this could change, so lets only copy when the flags tell us we can.
        if (bUpdateFlags & THROTTLE_FLAG) unThrottleIn = unThrottleInShared;
        if (bUpdateFlags & STEERING_FLAG) unSteeringIn = unSteeringInShared;
        if(bUpdateFlags & MOWER_FLAG) unMowerIn = unMowerInShared;
        bUpdateFlagsShared = 0; // clear shared copy of updated flags as we have already taken the updates we still have a local copy if we need to use it in bUpdateFlags
        interrupts();
      }

      String s = "";
      String s2 = "";
      if (bUpdateFlags & THROTTLE_FLAG) {
        //CRCArduinoFastServos::writeMicroseconds(SERVO_THROTTLE,unThrottleIn);
        //Serial.println(unThrottleIn);
        if (bUpdateFlags & STEERING_FLAG) {
          if (unSteeringIn > 1000) s="FL"; else s="FR";//GoTurnLeft(); else GoTurnRight();
        } else {
          if (unThrottleIn > 1000) s="F0"; else s="B0";//GoForward(); else GoBackward();
        }
      }
      if (bUpdateFlags & STEERING_FLAG) {
        //CRCArduinoFastServos::writeMicroseconds(SERVO_STEERING,unSteeringIn);
        if (unSteeringIn > 1000) s="0L"; else s="0R"; //GoTurnLeft(); else GoTurnRight();
      }
      if (bUpdateFlags & MOWER_FLAG) {
        //CRCArduinoFastServos::writeMicroseconds(SERVO_AUX,unAuxIn);
        if (unMowerIn > 1000) s2="M_FULL"; else s2="M_STOP";//MowerSpeed((int)MOWER_SPEED); else MowerSpeed((int)0);
       
       
        if (i < 1000) {
          arr[i] = unMowerIn;
          i++;
        } else {
          unsigned long sum = 0;
          for (int r=0; r < 1000; r++) {
            sum = sum + arr[r];
            if (arr[r] < minn) minn = arr[r];
            if (arr[r] > maxx) maxx = arr[r];
          }
          sum = sum / 1000;
          Serial.println("DONE");
          Serial.print("Avrg: ");
          Serial.print(avrg);
          Serial.print(" Min:");
          Serial.print(minn);
          Serial.print(" Max:");
          Serial.println(maxx);
          while (1<2) {delay(19);}
        }
       
      }
//      if (s != "") {
//        Serial.print("RC: S=");
//        Serial.print(unSteeringIn);
//        Serial.print(" T=");
//        Serial.print(unThrottleIn);
//        Serial.print(" M=");
//        Serial.println(unMowerIn);
//      }

      bUpdateFlags = 0;
}

void calcThrottle() {
  if (PCintPort::pinState) {
    unThrottleInStart = TCNT1;
  } else {
    unThrottleInShared = (TCNT1 - unThrottleInStart) >> 1;
    bUpdateFlagsShared |= THROTTLE_FLAG;
  }
}

void calcSteering() {
  if (PCintPort::pinState) {
    unSteeringInStart = TCNT1;
  } else {
    unSteeringInShared = (TCNT1 - unSteeringInStart) >> 1;
    bUpdateFlagsShared |= STEERING_FLAG;
  }
}

void calcMower() {
  if (PCintPort::pinState) {
    unMowerInStart = TCNT1;
  } else {
    unMowerInShared = (TCNT1 - unMowerInStart) >> 1;
    bUpdateFlagsShared |= MOWER_FLAG; 
  }
}

Thank you in advance...
3  Using Arduino / Programming Questions / Re: analogWrite(pin, 0) ouputs 0.104V? on: May 20, 2012, 03:17:37 am
I use the connection in attachment but without variac.
Other thing I noticed is if I use analogWrite(pin, 255) the motor is not fully ON (compared to speed achieved by linking gate of IRFZ via resistor in picture directly to +5V)?!
I call MowerSpeed(255); or with 0.

const int Mower = 12;

void MowerSpeed (unsigned char MowerSpeed) {
  //0-255   0=off  255=full on
  if ((SoftStart == (SoftBOTH - 1)) || (MowerSpeed > 127)) {
    for (unsigned char a = 0; a < MowerSpeed; a++) {
      analogWrite(Mower, a);
      delay(10);
    }
    SoftStart = 0;
  }
  analogWrite(Mower, MowerSpeed);
}

Tnx in advance.
4  Using Arduino / Programming Questions / Re: analogWrite(pin, 0) ouputs 0.104V? on: May 16, 2012, 03:35:30 pm
The pin is PCI28/OC1B/(Digital 12)/PD4/pin18 on ATMEL ATMEGA644P/SANGUINO.
So: pin=12.

I checked wiring.h and wiring_analog.c for sanguino and everything is OK. Translates to:
sbi(TCCR1A, COM1B1); // connect pwm to pin on timer 1, channel B
OCR1B = val; // set pwm duty

PWM works great at >0.
5  Using Arduino / Programming Questions / Re: analogWrite(pin, 0) ouputs 0.104V? on: May 16, 2012, 02:50:49 pm
Pure pin, only arduino/uC, hanging in the air.
6  Using Arduino / Programming Questions / analogWrite(pin, 0) ouputs 0.104V? on: May 16, 2012, 02:42:42 pm
Hi,
I am trying to drive a motor via PWM, but when I want to turn it off using analogWrite(pin, 0), I can stil hear hum from the motor. If I measure voltage on the PWM pin (nothing attached on it i.e. only uC pin) it reads 0.104V. It makes no change if I set the pin as output or not. How to solve it?

If this problem cannot be overcome, how to exchange analogWrite and digitalWrite? The following does not work:
pinMode(pin, OUTPUT); //=in Setup. Not needed for analogWrite
if (Speed == 0) {
 analogWrite(pin, 0);
 digitalWrite(pin, 0);
} else {
 //digitalWrite(pin, 1); ?!?!?!? what to do here?
 analogWrite(pin, Speed);
}
7  Forum 2005-2010 (read only) / Interfacing / AVR-NET-IO on: November 20, 2010, 08:56:22 am
Hi.
Has anyone tried Arduino on AVR-NET-IO from Pollin? ATmega644p. Any source code?
Pages: [1]