////////////////////////////////////////////////////////////////////////////////
// Official name: Bolderbot mini Speed encoders Single run Test 01 //
// Hardware platform: Bolderbot Mini //
// Pin connections: Arduino Mega 2560 //
// Created: April 2016 //
// Created by: HARB rboek2@gmail.com //
// http://uwvervoerder.nl/robotigs/includes/parts_header.php?idpart=197 //
// This program tests the interrupts on a MEGA 2560, but it should run on any //
// microcontroller that has at least 1 pin available for user interrupts. //
// Blocking the connected speed encoder will cause the LEDs to toggle //
////////////////////////////////////////////////////////////////////////////////
// FUSES (can always be altered by using the STK500) //
// On-Chip Debug Enabled: off (OCDEN=0) //
// JTAG Interface Enabled: off (JTAGEN=0) //
// Preserve EEPROM mem through the Chip Erase cycle: On (EESAVE = 0) //
// Boot Flash section = 2048 words, Boot startaddr=$3800 (BOOTSZ=00) //
// Boot Reset vector Enabled, default address=$0000 (BOOTSTR=0) //
// CKOPT fuse (operation dependent of CKSEL fuses (CKOPT=0) //
// Brown-out detection level at VCC=2,7V; (BODLEVEL=1) //
// Ext. Cr/Res High Freq.; Start-up time: 16K CK + 64 ms (CKSEL=1111 SUT=11) //
// LOCKBITS (are dangerous to change, since they cannot be reset) //
// Mode 1: No memory lock features enabled //
// Application Protect Mode 1: No lock on SPM and LPM in Application Section //
// Boot Loader Protect Mode 1: No lock on SPM and LPM in Boot Loader Section //
////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP: //
// Start End Number Description //
// 0000 0000 1 Never use this memory location to be AVR compatible //
////////////////////////////////////////////////////////////////////////////////
// PIN ALLOCATIONS //
// Prog=List=Chip //
// A0 = 97 = PF0 ADC0 = ADC //
// A1 = 96 = PF1 ADC1 = ADC //
// A2 = 95 = PF2 ADC2 = ADC //
// A3 = 94 = PF3 ADC3 = ADC //
// A4 = 93 = PF4 ADC4/TMK = ADC //
// A14 = 83 = PK6 ADC14/PCINT22 = ADC //
// A15 = 82 = PK7 ADC15/PCINT23 = ADC //
// 0 = 2 = PE0 RXD0/PCINT8 = Serial monitor, also on-board LED RX0 //
// 1 = 3 = PE1 TXD0 = Serial monitor, also on-board LED TX0 //
// 2 = 6 = PE4 OC3B/INT4 = INT //
// 3 = 7 = PE5 OC3C/INT5 = INT //
// 4 = 1 = PG5 OCOB = PWM //
// 5 = 5 = PE3 OC3A/AIN1 = PWM //
// 6 = 15 = PH3 OC4A = PWM //
// 7 = 16 = PH4 OC4B = PWM //
// 8 = 17 = PH5 OC4C = PWM //
// 9 = 18 = PH6 OC2B = PWM //
// 10 = 23 = PB4 OC2A/PCINT4 = PWM //
// 11 = 24 = PB5 OC1A/PCINT5 = PWM //
// 12 = 25 = PB6 OC1B/PCINT6 = PWM //
// 13 = 26 = PB7 OCOA/OC1C/PCINT7 = On board user LED, on=high off=low PWM //
// 18 = 46 = PD2 TXD1/INT3 = TX1 //
// 19 = 45 = PD2 RXD1/INT2 = RX1 //
// 20 = 44 = PD1 SDA/INT1 = Speed encoder Right TWI //
// 21 = 43 = PD0 SCL/INT0 = Speed encoder Left TWI //
// 44 = 40 = PL5 OC5C = 3 color led Blue PWM //
// 45 = 39 = PL4 OC5B = 3 color led Red PWM //
// 46 = 38 = PL3 OC5A = 3 color led Green PWM //
// 50 = 22 = PB3 MISO/PCINT3 = SPI //
// 51 = 21 = PB2 MOSI/PCINT2 = SPI //
// 52 = 20 = PB1 SCK/PCINT1 = SPI //
// 53 = 19 = PB1 SS/PCINT0 = SPI //
////////////////////////////////////////////////////////////////////////////////
// SET PRECOMPILER OPTIONS *****************************************************
// Initialse conditional compiling, uncomment to include, comment to exclude ---
// #define RS232 1 //Include RS232 sections to output debug info
// #ifdef RS232 //Only include this part if the variable has been defined
// Define precompiler variables -----------------(Runs faster & doesn`t use RAM)
#define LED PB7 //Arduino boards contain an onboard LED pin 13
#define SEL 2 //Speed Encoder Left on pin 21, binds to interrupt 2
#define SER 3 //Speed Encoder Right on pin 20, binds to interrupt 3
///Define the needed header files for the precompiler, no charge if not used ---
#include <TimerOne.h> //Currently needed for reading wheel speed per second
//#include <IRremote.h> //Do never use the default by the IDE but replace it
//#include <AFMotor.h> //Motors shield, Copyright Adafruit Industries LLC, 2009
//DEFINE VARIABLES -------------------------------------------------------------
//3 COLOR LED breakout, common ground, connect these pins preferably to PWM
const int ledRed = 44; //Define to which PWM pin this color is connected
const int ledGre = 45; //Define to which PWM pin this color is connected
const int ledBlu = 46; //Define to which PWM pin this color is connected
// Declare Variables ---
int Red = 4; //Brightness of this color, set by PWM 0=min 255=max
int Gre = 4; //Brightness of this color, set by PWM 0=min 255=max
int Blu = 4; //Brightness of this color, set by PWM 0=min 255=max
int inByte = 0; //For incoming serial data
int Color = 'R'; //The startup color to be faded
unsigned int cntr1 = 0; //Define left wheel counter per second
unsigned int cntr2 = 0; //Define right wheel counter per second
//END OF PRECOMPILER OPTIONS ---------------------------------------------------
void toggle_led(void) { //Toggles the default on-board LED on or off ***********
if bit_is_clear(PORTB, LED) { //Test if the onboard LED is off
PORTB |= (1 << LED); //If LED=off then turn LED on
Red = 0; //Brightness of this color, set by PWM 0=min 255=max
Gre = 50; //Brightness of this color, set by PWM 0=min 255=max
Blu = 0; //Brightness of this color, set by PWM 0=min 255=max
} else { //Else the LED must be on
PORTB &= ~(1 << LED); //Then turn the LED off
Red = 80; //Brightness of this color, set by PWM 0=min 255=max
Gre = 0; //Brightness of this color, set by PWM 0=min 255=max
Blu = 0; //Brightness of this color, set by PWM 0=min 255=max
} //End of if bit_is_clear(PORTB, LED) Or fe: state = !state;
analogWrite(ledRed, Red); //Set the brightness of this LED and illuminate it
analogWrite(ledGre, Gre); //Set the brightness of this LED and illuminate it
analogWrite(ledBlu, Blu); //Set the brightness of this LED and illuminate it
} //Exit toggle_led ------------------------------------------------------------
void doEncIsr() { //Increases the left wheel speed sensor by 1 *****************
cntr1++; //Increase +1 the counter value
toggle_led(); //Toggles the default on-board LED on or off
} //Exit docntLdoEncIsr---------------------------------------------------------
void timerIsr() { //Timer has reached its maximum *Interrupt Service Routine ***
Timer1.detachInterrupt(); //Stop the timer
toggle_led(); //Toggles the default on-board LED on or off
Serial.print(cntr1, DEC);
Serial.println(" pulses per 10 s");
cntr1 = 0;
Timer1.attachInterrupt( timerIsr ); //Enable the timer again
} //Exit timerIsr --------------------------------------------------------------
void setup() {
//pinMode(SEL, INPUT); //Redundant, set Speed Encoder Left as input (default)
pinMode(A15, INPUT_PULLUP);
//digitalWrite(20, HIGH); //Enable pullup resistor
digitalWrite(21, HIGH); //Enable pullup resistor
Timer1.initialize(10000000); //Timer creates an interrupt every 10 seconds
Timer1.attachInterrupt( timerIsr ); //Starts the timer counting
attachInterrupt(SER, doEncIsr, CHANGE); //Increase left cntr on any change
attachInterrupt(SEL, doEncIsr, CHANGE); //Increase left cntr on any change
Serial.begin(9600); //Nothing more needed for the Serial Monitor to function
DDRB |= (1 << LED); //Set onboard LED-pin as output
PORTB |= (1 << LED); //Initally onboard LED = on
pinMode(ledRed, OUTPUT); //Make the LED connections output pins
pinMode(ledGre, OUTPUT); //Make the LED connections output pins
pinMode(ledBlu, OUTPUT); //Make the LED connections output pins
} //End of setup
void loop() { //KEEP ON RUNNING THIS LOOP FOREVER\
toggle_led();
int sensorValue = analogRead(A15); // read the input on analog pin 0:
Serial.println(sensorValue); // print out the value you read:
delay (500);
} //End of void loop() //KEEP ON RUNNING THIS LOOP FOREVER
//345678911234567892123456789312345678941234567895123456789612345678971234567898