Show Posts
Pages: 1 ... 3 4 [5] 6 7 ... 43
61  Using Arduino / Microcontrollers / Re: Incorporating my code into a sleeping Atmega328P-AU on: July 02, 2013, 03:05:34 pm
Ok, thanks.
I just realized, I was planning to use 2 interrupts to wake the MCU, I guess this is ok? 
The first on D2 is from a RTC alarm.  The second is from a pushbutton switch.  I assume it's ok to just duplicate the process like this:
Code:
#include <avr/sleep.h>

 
void wake ()
{
  // cancel sleep as a precaution
  sleep_disable();
  // must do this as the pin will probably stay low for a while
  detachInterrupt (0);
  detachInterrupt (1);
}  // end of wake


void SleepNow () {
  ADCSRA = 0;    // disable ADC 
  set_sleep_mode (SLEEP_MODE_PWR_DOWN); 
  sleep_enable();

  // Do not interrupt before we go to sleep, or the
  // ISR will detach interrupts and we won't wake.
  noInterrupts ();
 
  attachInterrupt (0, wake, LOW);    // will be called when pin D2 goes low from RTC
  attachInterrupt (1, wake, LOW);    // will be called when pin D3 goes low from push button
 
  // turn off brown-out enable in software
  // BODS must be set to one and BODSE must be set to zero within four clock cycles
  MCUCR = _BV (BODS) | _BV (BODSE);
  // The BODS bit is automatically cleared after three clock cycles
  MCUCR = _BV (BODS);
 
  // We are guaranteed that the sleep_cpu call will be done
  // as the processor executes the next instruction after
  // interrupts are turned on.
  interrupts ();  // one cycle
  sleep_cpu ();   // one cycle
  }
62  Using Arduino / Microcontrollers / Re: Incorporating my code into a sleeping Atmega328P-AU on: July 02, 2013, 01:55:44 pm
I would just use this for the interrupt routine, right? 

Code:
void wake ()
{
  // cancel sleep as a precaution
  sleep_disable();
  // must do this as the pin will probably stay low for a while
  detachInterrupt (0);
}  // end of wake
63  Using Arduino / Microcontrollers / Re: Incorporating my code into a sleeping Atmega328P-AU on: July 02, 2013, 01:42:06 pm
...I see, I think.  So this code will stop executing at the very last line.
So I could put all of this code that is in the loop() routine into a function called something like SleepNow(), and then do my other code stuff in the loop(), and when I'm ready to go to sleep just call SleepNow().  Then when a LOW detect on the interupt pin comes in, the code execution will return from the SleepNow() routine, and continue on where it left off?
64  Using Arduino / Microcontrollers / Incorporating my code into a sleeping Atmega328P-AU on: July 02, 2013, 01:14:05 pm
I've read this page a few times and it's a great help:
http://www.gammon.com.au/forum/?id=11497

I've figured out I need this code below, that sleeps the MCU, and it wakes with a low signal on D2.  In my case, a RTC (DS3234) with an alarm set.
But I'm not sure what's happening when the MCU is sleeping. --is the loop() routine still running?  Where do I put my code that I want to execute when it awakes?  Here's a layout of what I want to happen:
SETUP()
   SHOW THE SPLASH SCREEN
   READ EEPROM USER SETTINGS
 LOOP()
   POWER UP THE RTC
   GET TIME/DATE
   SET NEXT ALARM
   POWER DOWN THE RTC (DS3234)
   POWER UP THE LCD
   DISPLAY NEXT ALARM FOR 2 SECONDS
   POWER DOWN THE LCD
   GO TO SLEEP
----------------------------------------

...NOW WHEN IT WAKES:
   POWER UP AN H-BRIDGE
   RUN A MOTOR
   POWER DOWN THE H-BRIDGE
   NOW DO EVERYTHING I WROTE IN LOOP()



 This is the code to have the MCU sleeping, if I understood what happens when the MUC goes to sleep, I might could figure out how to combine this all together.  When sleeping, does the loop() just get paused? If so where?


Code:
#include <avr/sleep.h>

const byte LED = 9;
 
void wake ()
{
  // cancel sleep as a precaution
  sleep_disable();
  // must do this as the pin will probably stay low for a while
  detachInterrupt (0);
}  // end of wake

void setup ()
  {
  digitalWrite (2, HIGH);  // enable pull-up
  }  // end of setup

void loop ()
{
 
  pinMode (LED, OUTPUT);
  digitalWrite (LED, HIGH);
  delay (50);
  digitalWrite (LED, LOW);
  delay (50);
  pinMode (LED, INPUT);
 
  // disable ADC
  ADCSRA = 0; 
 
  set_sleep_mode (SLEEP_MODE_PWR_DOWN); 
  sleep_enable();

  // Do not interrupt before we go to sleep, or the
  // ISR will detach interrupts and we won't wake.
  noInterrupts ();
 
  // will be called when pin D2 goes low 
  attachInterrupt (0, wake, LOW);
 
  // turn off brown-out enable in software
  // BODS must be set to one and BODSE must be set to zero within four clock cycles
  MCUCR = _BV (BODS) | _BV (BODSE);
  // The BODS bit is automatically cleared after three clock cycles
  MCUCR = _BV (BODS);
 
  // We are guaranteed that the sleep_cpu call will be done
  // as the processor executes the next instruction after
  // interrupts are turned on.
  interrupts ();  // one cycle
  sleep_cpu ();   // one cycle

  } // end of loop
65  Using Arduino / Motors, Mechanics, and Power / Re: Adding a 16x2 LCD in low quiescent mode on: June 28, 2013, 09:12:06 pm
You need to drive all the pins connected to the LCD low before turning off its power, then they won't draw any current.
Here is a test code I made to be sure I'm properly powering up/down my LCD.  How does it look?  I don't want to test it on my LCD in case it's wrong and I mess up my LCD.

Code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(A7,A0,A2,A3,A4,A5);
int LCDpins[] = {
  A7, A0, A2, A3, A4, A5};
int LCDpower = 4; //pin controlling LCD power
unsigned long LCDtimer;  //counter

void setup() {
  //don't use LCD.begin() here, correct?
  pinMode(LCDpower, OUTPUT);  //set pin as output
  digitalWrite(LCDpower, LOW); //maybe not needed?
}

void loop() {
  if (millis() - LCDtimer > 10000) {  //every 10 seconds
    LCDtimer = millis();  //update counter
    display_something();  //activate the LCD
  }
  //do other stuff...
  delay(100);

}

void display_something() {
  digitalWrite(LCDpower, HIGH); //power up the LCD
  lcd.begin(16, 2); //start the LCD
  lcd.clear(); //clear the screen
  lcd.print(millis());  //print the time
  delay(2000); //give user time to read it
  for (int i = 0; i < 6; i++) {
    digitalWrite(LCDpins[i], LOW);
  }
  digitalWrite(LCDpower, LOW);  //power down the LCD
}

66  Using Arduino / Networking, Protocols, and Devices / Partial answer on: June 18, 2013, 12:35:58 pm
I discovered that with the slower Serial Comm speed, this code is never getting into this while statement:

Code:
while (!mpuInterrupt && fifoCount < packetSize) {
    // other program behavior stuff here
    update_motor();
 }

If I move my routine outside of this while statement, it works.  I the serial monitor, I get a fifo overflow error, but it still works and gives me good readings, so I guess this doesn't matter.  I think I vaguely understand the problem--that the slower serial speed is making it not be able to keep up with the data coming from the IMU?  Anyway, I don't think I need data that fast for my application, so I guess the fifo overflow won't matter?
67  Using Arduino / Networking, Protocols, and Devices / Re: MPU-6050 not working with slow serial comm on: June 18, 2013, 11:11:11 am
PS.  That softwareSerial call @ 9600 make it not work too.  If I change it to higher or just remove it, it'll work.  Once I get it working, I won't be using Serial, only that SoftwareSerial call.
68  Using Arduino / Networking, Protocols, and Devices / MPU-6050 not working with slow serial comm on: June 18, 2013, 11:08:45 am
I'm having strange issues for the MPU-6050, using the I2Cdev & MPU-6050 libraries from Jeff R.
It all works fine if my Serial port is 115200, but at lower speeds it hangs up on the "Initializing I2C devices..." message.
At 38400 it sometimes start and sometime doesn't.  This is the faster baud I can use (using the Sabertooth 2x25)
I really don't understand all the workings of this, but I wonder if there's a way to slow down whatever Arduino /MPU-6050 communications or something.  I don't really need reading as fast as it gives them.  My first version of a balancing bot worked great with readings only every 50ms.
Here's my code, it all seems working @ 115200 baud (only tested with a monitor reading since I can't communicate with the sabertooth).
I'm really stumped!
thanks
Code:
#include "Wire.h"
// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
//#include "MPU6050.h" // not necessary if using MotionApps include file
MPU6050 mpu;
/* =========================================================================
 NOTE: In addition to connection 3.3v, GND, SDA, and SCL, this sketch
 depends on the MPU-6050's INT pin being connected to the Arduino's
 external interrupt #0 pin.
/* =========================================================================
 // uncomment "OUTPUT_READABLE_YAWPITCHROLL" if you want to see the yaw/
 // pitch/roll angles (in degrees) calculated from the quaternions coming
 // from the FIFO. Note this also requires gravity vector calculations.
 // Also note that yaw/pitch/roll angles suffer from gimbal lock (for
 // more info, see: http://en.wikipedia.org/wiki/Gimbal_lock)
 */
#define OUTPUT_READABLE_YAWPITCHROLL
// MPU control/status vars
bool dmpReady = false;  // set true if DMP init was successful
uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount;     // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer
// orientation/motion vars
Quaternion q;           // [w, x, y, z]         quaternion container
VectorInt16 aa;         // [x, y, z]            accel sensor measurements
VectorInt16 aaReal;     // [x, y, z]            gravity-free accel sensor measurements
VectorInt16 aaWorld;    // [x, y, z]            world-frame accel sensor measurements
VectorFloat gravity;    // [x, y, z]            gravity vector
float euler[3];         // [psi, theta, phi]    Euler angle container
float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
// ================================================================
// ===               INTERRUPT DETECTION ROUTINE                ===
// ================================================================
volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
  mpuInterrupt = true;
}
// ================================================================
// ===                      KEENEWAY SETUP                      ===
// ================================================================
#include <SoftwareSerial.h>
SoftwareSerial Segway(4,12); //(4, 12); // RX, TX, (D12 is used to send commands to the Sabertooth)
boolean debugMode = false;  //set to true to execute Serial debug data
int pitch, turn;

// ================================================================
// ===                      INITIAL SETUP                       ===
// ================================================================

void setup() {
  // join I2C bus (I2Cdev library doesn't do this automatically)
  Wire.begin();

  // initialize serial communication
  Serial.begin(115200);
  Segway.begin(9600);         //begin communication with Segway motor driver
  // initialize device
  Serial.println(F("Initializing I2C devices..."));
  mpu.initialize();

  // verify connection
  Serial.println(F("Testing device connections..."));
  Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));

  // wait for ready
  Serial.println(F("\nSend any character to begin DMP programming and demo: "));
  while (Serial.available() && Serial.read()); // empty buffer
  //while (!Serial.available());                 // wait for data
  while (Serial.available() && Serial.read()); // empty buffer again

  // load and configure the DMP
  Serial.println(F("Initializing DMP..."));
  devStatus = mpu.dmpInitialize();

  // make sure it worked (returns 0 if so)
  if (devStatus == 0) {
    // turn on the DMP, now that it's ready
    Serial.println(F("Enabling DMP..."));
    mpu.setDMPEnabled(true);

    // enable Arduino interrupt detection
    Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
    attachInterrupt(0, dmpDataReady, RISING);
    mpuIntStatus = mpu.getIntStatus();

    // set our DMP Ready flag so the main loop() function knows it's okay to use it
    Serial.println(F("DMP ready! Waiting for first interrupt..."));
    dmpReady = true;

    // get expected DMP packet size for later comparison
    packetSize = mpu.dmpGetFIFOPacketSize();
  }
  else {
    // ERROR!
    // 1 = initial memory load failed
    // 2 = DMP configuration updates failed
    // (if it's going to break, usually the code will be 1)
    Serial.print(F("DMP Initialization failed (code "));
    Serial.print(devStatus);
    Serial.println(F(")"));
  }
}



// ================================================================
// ===                    MAIN PROGRAM LOOP                     ===
// ================================================================

void loop() {
  // if programming failed, don't try to do anything
  if (!dmpReady) return;

  // wait for MPU interrupt or extra packet(s) available
  while (!mpuInterrupt && fifoCount < packetSize) {
    // other program behavior stuff here
    update_motor();
    }

    // reset interrupt flag and get INT_STATUS byte
    mpuInterrupt = false;
  mpuIntStatus = mpu.getIntStatus();

  // get current FIFO count
  fifoCount = mpu.getFIFOCount();

  // check for overflow (this should never happen unless our code is too inefficient)
  if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
    // reset so we can continue cleanly
    mpu.resetFIFO();
    if (debugMode) {
      Serial.println(F("FIFO overflow!"));
    }
    // otherwise, check for DMP data ready interrupt (this should happen frequently)
  }
  else if (mpuIntStatus & 0x02) {
    // wait for correct available data length, should be a VERY short wait
    while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();

    // read a packet from FIFO
    mpu.getFIFOBytes(fifoBuffer, packetSize);

    // track FIFO count here in case there is > 1 packet available
    // (this lets us immediately read more without waiting for an interrupt)
    fifoCount -= packetSize;

#ifdef OUTPUT_READABLE_YAWPITCHROLL
    // display Euler angles in degrees
    mpu.dmpGetQuaternion(&q, fifoBuffer);
    mpu.dmpGetGravity(&gravity, &q);
    mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
    pitch = (int(ypr[1] * 180/M_PI));
    turn = (int(ypr[2] * 180/M_PI));
    if (debugMode) {
      Serial.print("Pitch\t");
      Serial.print(int(ypr[1] * 180/M_PI));
      Serial.print("\tturn\t");
      Serial.println(int(ypr[2] * 180/M_PI));
    }
#endif
  }
}

void update_motor(){  // Update the motors
  int maxPitch = 30;
  int maxTurn = 10; //the max angle for the most turn
  turn = constrain(turn, -maxTurn, maxTurn);

  if (pitch > 60) crash(); //we probably crashed
  if (pitch > maxPitch) tooFast(); //we are going too fast, so try to slow it down
  int motor_speed = map(pitch, -maxPitch, maxPitch, -64, 64); // map the angle to the sabertooth range 1-64  
  int motor1 = motor_speed + (turn);  //add steering bias to motor 1
  int motor2 = motor_speed - (turn);  //add steering bias to motor 2
  // assign final motor output values
  motor1 = 64 + motor1;                //64 is neutral for motor 1
  motor2 = 192 + motor2;               //192 is neutral for motor 2
  motor1 = constrain(motor1, 1, 127);  //constrain the value to it's min/max
  motor2 = constrain(motor2, 128, 255);//constrain the value to it's min/max
  Serial.print(motor1); //Send motor 1 speed over serial
  Serial.print("\t");
  //delay(1);  //sabertooth can only receive commands at 2000/second
  Serial.println(motor2);  //Send motor 2 speed over serial
}

void turnAdjust() { //return turn adjust for speed
  int maxTurn = 10; //the max angle for the most turn
  turn = constrain(turn, -maxTurn, maxTurn);
}
69  Using Arduino / Sensors / Re: MPU-6050 gyro calibration on: June 17, 2013, 01:53:22 pm
Thanks!  I've heard of processing but I never knew what it was.
70  Using Arduino / Sensors / Re: MPU-6050 gyro calibration on: June 17, 2013, 01:26:43 pm
About the photos in this thread, they look like a program that takes the YPR and simulates it on your computer.  Is there any kind of free programs that would do this for me on my Mac, using the MPU-6050?  I've searched for motion tracking software with no luck, that doesn't seem to be the right wording...
71  Using Arduino / Sensors / MPU6050 class using DMP (MotionApps v2.0) on: June 17, 2013, 01:14:18 pm
On my homemade Segway, I first used an analog 5DoF board, but now I've switched to a MPU6050 class using DMP (MotionApps v2.0). 
With the analog one, I had to "calibrate" it with an accel offset, and gyro offset.  But this one, using Jeff R.'s libraries, seems to do all the computing, and one output give the ypr[] which is in degrees.  I had to do a lot of computing, along with an offset to get this number with the analog 5DoF, so this is a lot easier. 
My question:  It appears that I don't have to recalibrate it with some kind of offset like I did the analog one, is this correct?  It will just put out the correct ypr each time I turn it on?
thanks.
72  Using Arduino / Networking, Protocols, and Devices / Re: GSM carrier in USA for Arduino tinkering on: June 06, 2013, 08:20:01 pm
Thanks. I got a couple free activation kits. I see you can get a $10 card that last 3 months so that not bad to test out a shield for 25¢ a text.
73  Using Arduino / Motors, Mechanics, and Power / Re: SMD MOSFET to power down my SMD H-Bridge on: June 05, 2013, 10:54:58 pm
Running from 9V, you definitely need R1, R2 to be 2 x 1K. However, your schematic is wrong. They need to be in series, not in parallel, and the gate drive to the P-channel mosfet needs to be taken from the junction of the two.

I would connect the 100nF capacitor to the source terminal of the P-channel mosfet instead of the drain terminal, and put a much larger electrolytic capacitor (e.g. 1000uF) in parallel with it. Position the P-channel mosfet and capacitors close to the 9986, and keep the traces connecting these 4 components short.

I'm finishing up my board to send off to the fab house.  Everything on this board is very small, except that 1000uF CAP.  Is that correct, a 1000uF size?  Just checking, thanks.


74  Using Arduino / Networking, Protocols, and Devices / Re: GSM carrier in USA for Arduino tinkering on: June 05, 2013, 09:24:45 am
Thanks for the info.
75  Using Arduino / Networking, Protocols, and Devices / GSM carrier in USA for Arduino tinkering on: June 05, 2013, 12:10:14 am
I picked up a gsm shield to tinker with and am wondering if anyone knows a cheap pre-paid carrier in the US. A few years ago I got a CDMA activation/number off eBay for lik .99¢, and it came with a 100 free minutes good for 3 months. I'd like to find some kind of pre paid SIM card on the GSM network, but the cheapest I've found is more like 10-15$ a month.
Thanks,
Pages: 1 ... 3 4 [5] 6 7 ... 43