Sunday, June 7, 2015

How to distinguish Arduino's models at compile time?




This is a question that you probably already asked yourself when trying to write code that must work with different Arduino's variants. Indeed, when using I2C, timers or PWM, the pins concerned on the Arduino Uno for these operations are different from Arduino Mega or form Due, etc. Hence, how to avoid the multiplication of the source files, of the classes or to excessively overload functions?

Solution: use conditional compilation!

In this short note we try to explain how to do that. Indeed, digging into the avr include files (see io.h for example) one can find the following constants are used:

__AVR_ATmega328P__
__AVR_ATmega328__

These are used to recognize that we are compiling for Arduino Uno. For the Arduino Mega we find:

__AVR_ATmega1280__
__AVR_ATmega2560__

Therefore your code might look like:
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328P__)
  
  [your arduino uno code here]
  
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)

  [your arduino mega code here]


Enjoy!

Sunday, March 15, 2015

PIR sensors: exploiting passivity




We have already spoken about the PIR sensor in these pages (see here) and many other pointers can be found on the web. However, the example sketches does never consider the main aspect of this sensor: passivity! The sketches always employ busy waiting (ie they continuously loop on the test for changes over the alarm pin).

The PIR sensor used in the sketch.


The idea

Use interrupts! The idea is that the alarm function is activated by the state change of the signal pin of the sensor. In this way the CPU can perform other tasks when the alarm is not activated.

Remark

When the alarm is triggered on the PIR sensor it remains in this state for a certain amount of time. Since we chose to implement the CHANGE option for the interrupt routine we have to prevent that the service routine is run twice instead of once. This is implemented using a simple flag.

The sketch

Since the sketch has only prototyping purposes, the only effect of the alarm trigger is to print a message to the serial monitor. To see the message open the serial monitor, choose the right port and communication speed.

/**
 * PIRpassExploit
 *
 * A convenient way to use PIR sensors
 *
 * \author Enrico Formenti
 * \version 1.0
 * \date 15 March 2015
 * \copyright BSD license, check the License page on the blog for
 * more information. All this text must be included in any 
 * redistribution.
 * <br><br>
 * See macduino.blogspot.com for more details.
 *
 * Remarks:
 * - OUT pin is connected to digital pin 2 of Arduino, change this
 *   if needed
 * - DELAY times depend on the type of module and/or its 
 *   configuration.
 */

// OUT pin on PIR sensor connected to digital pin 2
// (any other digital pin will do, just change the value below)
#define PIRSensorPin 2

// This is the amount that the sensor takes to setup internals
// Check your sensor datasheet for the correct value
// Mine is about 17 seconds
#define PIR_INIT_DELAY 17000

// This is used to avoid triggering alarm twice when signal returns
// to normal state
volatile bool alarmUnarmed;

void doAlarm() {
  if(alarmUnarmed) {
    alarmUnarmed = false;    
    Serial.println("Alarm triggered!");
  }
  else
    alarmUnarmed = true;
}

void setup() {
  Serial.begin(9600);
  
  pinMode(PIRSensorPin, INPUT);
  delay(PIR_INIT_DELAY);
  
  alarmUnarmed = true;
  attachInterrupt(0, doAlarm, CHANGE);
}

void loop() {

  // do some good work here ;-)
}

See also


Enjoy!

Saturday, February 1, 2014

avrdude: stk500(): programmer is not responding




Oh my god!

Is my beloved Arduino gone?

That was the question I was asking me when I saw those fatal words on the Arduino IDE. Indeed, I was trying to use some servos and I was afraid that there some current overload have occurred on some pins. I immediately unwired all the motors and started thinking.

The solution
After some trials I realized! I just connected the Arduino to the left usb port instead of the usual one on the right side of my Mac. These two are mapped to different ports of course and the IDE was still looking for the right one. Indeed, the IDE memorizes the last used port. I changed the name of the port in the menu "Tools" and that solved the problem! This time I was lucky...

Things to try
If you have the same error, you can try the following (increasing difficulty):

  1. verify the usb cable (one never knows ;-) )
  2. change the port in the menu "Tools"
  3. take off the ATMEL chip and verify if the chip is ok
  4. if the chip is ok, try to load the bootloader again

I'm afraid that if none of the above steps works you should replace your Arduino!

Hope it helps!

Friday, May 3, 2013

What that blinking LED?




At first power on I saw a blinking led on Arduino board and got immediately worried about it! I rushed to the internet and after some surfing I found the answer. In most boards sold over the internet, Arduino come with a pre-loaded program which is "Blink". The program can be found in the examples provided with the Arduino IDE, I report it below. Just upload another program to get the LED stopped.

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);    
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}
This was the first experience hopefully the next ones will be less threatening and more exciting ;-)

How all started




I recently came up on this page www.arduino.cc. I got fulgurated. A simple playground for electronic/robotic hobbyist, at last! I immediately rushed to xybay to buy the card dreaming of fantastic applications and of the most futuristic robotic scenarios.

This is open hardware! That is the hardware version of open software. I immediately bought lots of small add-ons and read lots of stuff.I will try to record here my progress and small discoveries.