Arduino RGB LED Control from 2 pots - arduino

I'm trying to write some code that reads a voltage from 2 different pots and converts that to 3 pwm outputs that I can then send to an RGB LED. My idea is to use something like a colour map that is used to plot complex functions, but I'm not sure how to implement that. Any suggestions?
#define COLOUR_POT_INPUT 4
#define INTENSITY_POT_INPUT 3
#define LED_RED 9
#define LED_GREEN 10
#define LED_BLUE 11
float colour_angle;
float colour_radius;
float colour_x_value;
float colour_y_value;
int red_value;
int green_value;
int blue_value;
const float pi = 3.1415;
void setup() {
pinMode(COLOUR_POT_INPUT, INPUT);
pinMode(INTENSITY_POT_INPUT, INPUT);
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
}
void loop() {
colour_angle = analogRead(COLOUR_POT_INPUT);
colour_radius = analogRead(INTENSITY_POT_INPUT);
colour_angle = map(colour_angle, 0, 1023, 0, 2*pi);
colour_radius = map(colour_radius, 0, 1023, 0, 255);
colour_x_value = colour_radius*cos(colour_angle);
colour_y_value = colour_radius*sin(colour_angle);
}
//Insert function here that maps colour x and y value to red green blue
value

Assuming colour_angle and colour_radius represent hue and saturation respectively, then you can use any HSL/HSV-to-RGB conversion code, with a fixed lightness/value.
Picking randomly from google results:
HSL to RGB conversion
HSV to RGB conversion

Related

Stepper motor does not work when used with ultrasonic sensor and step motor in Arduino

When the ultrasonic sensor determines that there is an object within a certain range, I want to make the step motor move once, but the step motor does not move. The step motor lights up, but I don't know what the problem is. Except for the conditional syntax and ultrasonic sensor, the step motor works well when you run the step motor.
The code is as follows:
#include <AccelStepper.h>
int a;
int trigPin4 = A1;
int echoPin4 = A2;
long duration4, distance4;
// Define step constants
#define FULLSTEP 4
#define HALFSTEP 8
#define motorPin1 8
#define motorPin2 9
#define motorPin3 10
#define motorPin4 11
#define motorPin5 4
#define motorPin6 5
#define motorPin7 6
#define motorPin8 7
AccelStepper stepper1(FULLSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(FULLSTEP, motorPin5, motorPin7, motorPin6, motorPin8);
void setup()
{
pinMode(trigPin4, OUTPUT);
pinMode(echoPin4, INPUT);
Serial.begin(9600);
// 1 revolution Motor 1 CW
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(50.0);
stepper1.setSpeed(100);
stepper1.moveTo(2048);
// 1 revolution Motor 2 CCW
stepper2.setMaxSpeed(1000.0);
stepper2.setAcceleration(50.0);
stepper2.setSpeed(100);
stepper2.moveTo(-2048);
}
void loop()
{
digitalWrite(trigPin4, LOW);
delayMicroseconds(2);
digitalWrite(trigPin4, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin4, LOW);
duration4 = pulseIn(echoPin4, HIGH);
distance4 = duration4 * 0.034 / 2;
if (distance4 >= 500 || distance4 <= 0) {
Serial.println("Out of range");
}
else {
Serial.print("Sensor4 : ");
Serial.print(distance4);
Serial.println("cm");
a = 1;
}
if (a == 1) {
stepper1.run();
stepper2.run();
}
delay(2000);
}
first, do not use "delay" function.
Second, put a= 0 to the first if statement because in your code, once the distance satisfies the the else condition the value of a will remain 1 forever

How to fix 'ANGLES was not declared in this scope' if it's declared

This is not my code but I don't get why it doesn't work. The define function is used to declare NUM ANGLES but it says that ANGLES is not declared.
I have tried to put the #define in all the scopes but this didn't work
#define NUM ANGLES 7
Does it help when I only declare ANGLES or something. This is the first time I'm using the arduino car kit 2wd and it's quite difficult.
/*
* Firmware for the ”2WD Ultrasonic Motor Robot Car Kit”
*
* Stephen A. Edwards
*
* Hardware configuration :
* A pair of DC motors driven by an L298N H bridge motor driver
* An HC−SR04 ultrasonic range sensor mounted atop a small hobby servo
*/
#include <Servo.h>
Servo servo;
// Ultrasonic Module pins
const int trigPin = 13; // 10 microsecond high pulse causes chirp , wait 50 us
const int echoPin = 12; // Width of high pulse indicates distance
// Servo motor that aims ultrasonic sensor .
const int servoPin = 11; // PWM output for hobby servo
// Motor control pins : L298N H bridge
const int enAPin = 6; // Left motor PWM speed control
const int in1Pin = 7; // Left motor Direction 1
const int in2Pin = 5; // Left motor Direction 2
const int in3Pin = 4; // Right motor Direction 1
const int in4Pin = 2; // Right motor Direction 2
const int enBPin = 3; // Right motor PWM speed control
enum Motor { LEFT, RIGHT };
// Set motor speed: 255 full ahead, −255 full reverse , 0 stop
void go( enum Motor m, int speed)
{
digitalWrite (m == LEFT ? in1Pin : in3Pin , speed > 0 ? HIGH : LOW );
digitalWrite (m == LEFT ? in2Pin : in4Pin , speed <= 0 ? HIGH : LOW );
analogWrite(m == LEFT ? enAPin : enBPin, speed < 0 ? -speed : speed );
}
// Initial motor test :
// left motor forward then back
// right motor forward then back
void testMotors ()
{
static int speed[8] = { 128, 255, 128, 0 , -128, -255, -128, 0 };
go(RIGHT, 0);
for (unsigned char i = 0 ; i < 8 ; i++)
go(LEFT, speed[i ]), delay (200);
for (unsigned char i = 0 ; i < 8 ; i++)
go(RIGHT, speed[i ]), delay (200);
}
// Read distance from the ultrasonic sensor , return distance in mm
//
// Speed of sound in dry air , 20C is 343 m/s
// pulseIn returns time in microseconds (10ˆ−6)
// 2d = p * 10ˆ−6 s * 343 m/s = p * 0.00343 m = p * 0.343 mm/us
unsigned int readDistance ()
{
digitalWrite ( trigPin , HIGH );
delayMicroseconds (10);
digitalWrite ( trigPin , LOW );
unsigned long period = pulseIn ( echoPin, HIGH );
return period * 343 / 2000;
}
#define NUM ANGLES 7
unsigned char sensorAngle[NUM ANGLES] = { 60, 70, 80, 90, 100, 110, 120 };
unsigned int distance [NUM ANGLES];
// Scan the area ahead by sweeping the ultrasonic sensor left and right
// and recording the distance observed. This takes a reading , then
// sends the servo to the next angle. Call repeatedly once every 50 ms or so.
void readNextDistance ()
{
static unsigned char angleIndex = 0;
static signed char step = 1;
distance [angleIndex ] = readDistance ();
angleIndex += step ;
if (angleIndex == NUM ANGLES - 1) step = -1;
else if (angleIndex == 0) step = 1;
servo . write ( sensorAngle[angleIndex ] );
}
// Initial configuration
//
// Configure the input and output pins
// Center the servo
// Turn off the motors
// Test the motors
// Scan the surroundings once
//
void setup () {
pinMode(trigPin , OUTPUT);
pinMode(echoPin, INPUT);
digitalWrite ( trigPin , LOW);
pinMode(enAPin, OUTPUT);
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(in3Pin, OUTPUT);
pinMode(in4Pin, OUTPUT);
pinMode(enBPin, OUTPUT);
servo . attach ( servoPin );
servo . write (90);
go(LEFT, 0);
go(RIGHT, 0);
testMotors ();
// Scan the surroundings before starting
servo . write ( sensorAngle[0] );
delay (200);
for (unsigned char i = 0 ; i < NUM ANGLES ; i ++)
readNextDistance (), delay (200);
}
// Main loop:
//
// Get the next sensor reading
// If anything appears to be too close , back up
// Otherwise, go forward
//
void loop () {
readNextDistance ();
// See if something is too close at any angle
unsigned char tooClose = 0;
for (unsigned char i = 0 ; i < NUM ANGLES ; i++)
if ( distance [ i ] < 300)
tooClose = 1;
if (tooClose) {
// Something's nearby: back up left
go(LEFT, -180);
go(RIGHT, -80);
}
else {
// Nothing in our way: go forward
go(LEFT, 255);
go(RIGHT, 255);
}
// Check the next direction in 50 ms
delay (50);
}
You could not use spaces in the first part of the define
#define NUM_ANGLES 7
Define is nothing more than a text replacement that replaces the first token with the rest of the line.
You can't use spaces when delcaring a constant using a define. This is because a define is used by the preprocessor and not the compiler.
You have written:
#define NUM ANGLES 7
...
for (unsigned char i = 0 ; i < NUM ANGLES ; i ++)
The preprocessor converts this into a piece of text that the compiler will use to generate a executable program. The current preprocessor output of that piece is:
for (unsigned char i = 0 ; i < ANGLES 7 ANGLES ; i ++)
Because NUM is replaced with ANGELS and as such the compiler is looking for a variable named ANGELS.
If you write the NUM ANGLES without spaces: (and replace all other places where a space is incorrect)
#define NUM_ANGLES 7
Then preprocessor result will look like this:
for (unsigned char i = 0 ; i < 7 ; i ++)
That is indeed compilable code.
This all is because the format of a define is as follows:
#define identifier token-string newline

Why is TCNT1 not counting up on Atmega328?

I have the following code for the Arduino with Atmega328 and a common 16x2 LCD. The LCD is working, but it is always showing the starting value "333" of the Timer 1 counter TCNT1. Why? I have read the datasheet of the 328 over and over again, but I don't get it.
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int lcdContrastPin = 6, lcdBackligthPin = 10;
void setup()
{
// tutn on LCD backlight and contrast
pinMode(lcdContrastPin, OUTPUT);
pinMode(lcdBackligthPin, OUTPUT);
// fine-tuning contrast could be done by PWM on lcdContrastPin
digitalWrite(lcdContrastPin, LOW);
digitalWrite(lcdBackligthPin, HIGH);
lcd.begin(16, 2);
// configure Timer1
TCCR1A = 0; // no waveform generation
TCCR1B = 0x00000010; // frequency divider 8 (i.e. counting with 2 MHz)
TCCR1C = 0;
TIFR1 = 0x00100000; // clear Input Capture Flag
TCNT1 = 333;
}
void loop()
{
int currentTimerValue = TCNT1;
lcd.setCursor(0, 0);
lcd.print("TCNT1=");
lcd.print(currentTimerValue);
lcd.println(" ");
delay(50);
}
Stupid me! In a lapse of consciousness I took 0x00000010 as a binary number instead of as a hexadecimal which it is. As a result I set the all clock selection bits to 0 which means the timer stops.
After replacing 0x00000010 by 0b00000010 (the true binary number) everything works as expected now:
TCCR1B = 0b00000010; // frequency divider 8 (i.e. counting with 2 MHz)
TCCR1C = 0;
TIFR1 = 0b00100000; // clear Input Capture Flag

FSR presses to increase variable on a display

Update: So I'm getting the error: "expected initializer before 'fsrAnalogPin'" with this code. Can someone help me sort this out?
// testshapes demo for Adafruit RGBmatrixPanel library.
// Demonstrates the drawing abilities of the RGBmatrixPanel library.
// For 32x32 RGB LED matrix:
// http://www.adafruit.com/products/607
// Written by Limor Fried/Ladyada & Phil Burgess/PaintYourDragon
// for Adafruit Industries.
// BSD license, all text above must be included in any redistribution.
#include <Adafruit_GFX.h> // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library
// If your 32x32 matrix has the SINGLE HEADER input,
// use this pinout:
#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
#define OE 9
#define LAT 10
#define A A0
#define B A1
#define C A2
#define D A3
#define PULLUP true //use the AVR's internal pullup resistor
#define INVERT true //low level means fsr pressed
#define DEBOUNCE_TIME 50 //milliseconds
// If your matrix has the DOUBLE HEADER input, use:
//#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
//#define LAT 9
//#define OE 10
//#define A A3
//#define B A2
//#define C A1
//#define D A0
RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);
void loop(){
Serial.begin(9600);
matrix.begin();
// draw a pixel in solid white
matrix.drawPixel(0, 0, matrix.Color333(7, 7, 7));
delay(500);
// fix the screen with green
matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 7, 0));
delay(500);
// draw a box in yellow
matrix.drawRect(0, 0, 32, 32, matrix.Color333(7, 7, 0));
delay(500);
// draw an 'X' in red
matrix.drawLine(0, 0, 31, 31, matrix.Color333(7, 0, 0));
matrix.drawLine(31, 0, 0, 31, matrix.Color333(7, 0, 0));
delay(500);
// draw a blue circle
matrix.drawCircle(10, 10, 10, matrix.Color333(0, 0, 7));
delay(500);
// fill a violet circle
matrix.fillCircle(21, 21, 10, matrix.Color333(7, 0, 7));
delay(500);
// fill the screen with 'black'
matrix.fillScreen(matrix.Color333(0, 0, 0));
// draw some text!
matrix.setCursor(1, 0); // start at top left, with one pixel of spacing
matrix.setTextSize(1); // size 1 == 8 pixels high
matrix.setTextWrap(false); // Don't wrap at end of line - will do ourselves
matrix.setTextColor(matrix.Color333(7,7,7));
matrix.println("FIST");
matrix.println(" BUMP");
// print each letter with a rainbow color
matrix.setTextColor(matrix.Color333(7,0,0));
matrix.print('C');
matrix.setTextColor(matrix.Color333(7,4,0));
matrix.print('O');
matrix.setTextColor(matrix.Color333(7,7,0));
matrix.print('U');
matrix.setTextColor(matrix.Color333(4,7,0));
matrix.print('N');
matrix.setTextColor(matrix.Color333(0,7,0));
matrix.println('T');
{
const int fsr_pin = A4; //connect fsr from this pin to ground
int read fsrAnalogPin = 4;
fsr_pin.read();
if( fsr_pin.wasPressed() ){
counter = counter + 1
matrix.setTextColor( matrix.Color333(0,7,7) );
matrix.print( counter )
}
}
// whew!
}
void loop() {
// do nothing
}
if someone can help me with my code I would be very grateful. What I'm trying to do is: Using a force-sensitive resistor, I want to count a given amount of pressure on the FSR with an arduino, that then displays each tap on an LED matrix. In other words, I have a glove with an FSR on it, and I want to count "fist bumps" that then display on an 32x32 Adafruit LED matrix.
Here's an example of what I want it to look like:
https://drive.google.com/file/d/0B4kq-ADrtz4mSzNWUTJoNGlBSU0/view?usp=sharing
That is just a static number on the bottom though. I need help making the code to connect a counting variable to the FSR "bumps." It's probably pretty simple but I'm entirely new to this so it's a lot to learn. So any help is appreciated
Here is what I have for code:
// testshapes demo for Adafruit RGBmatrixPanel library.
// Demonstrates the drawing abilities of the RGBmatrixPanel library.
// For 32x32 RGB LED matrix:
// http://www.adafruit.com/products/607
// Written by Limor Fried/Ladyada & Phil Burgess/PaintYourDragon
// for Adafruit Industries.
// BSD license, all text above must be included in any redistribution.
#include <Adafruit_GFX.h> // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library
// If your 32x32 matrix has the SINGLE HEADER input,
// use this pinout:
#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
#define OE 9
#define LAT 10
#define A A0
#define B A1
#define C A2
#define D A3
#define FSR_PIN A4 //connect fsr from this pin to ground
#define PULLUP true //use the AVR's internal pullup resistor
#define INVERT true //low level means fsr pressed
#define DEBOUNCE_TIME 50 //milliseconds
// If your matrix has the DOUBLE HEADER input, use:
//#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
//#define LAT 9
//#define OE 10
//#define A A3
//#define B A2
//#define C A1
//#define D A0
RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);
void setup() {
Serial.begin(9600);
matrix.begin();
// draw a pixel in solid white
matrix.drawPixel(0, 0, matrix.Color333(7, 7, 7));
delay(500);
// fix the screen with green
matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 7, 0));
delay(500);
// draw a box in yellow
matrix.drawRect(0, 0, 32, 32, matrix.Color333(7, 7, 0));
delay(500);
// draw an 'X' in red
matrix.drawLine(0, 0, 31, 31, matrix.Color333(7, 0, 0));
matrix.drawLine(31, 0, 0, 31, matrix.Color333(7, 0, 0));
delay(500);
// draw a blue circle
matrix.drawCircle(10, 10, 10, matrix.Color333(0, 0, 7));
delay(500);
// fill a violet circle
matrix.fillCircle(21, 21, 10, matrix.Color333(7, 0, 7));
delay(500);
// fill the screen with 'black'
matrix.fillScreen(matrix.Color333(0, 0, 0));
// draw some text!
matrix.setCursor(1, 0); // start at top left, with one pixel of spacing
matrix.setTextSize(1); // size 1 == 8 pixels high
matrix.setTextWrap(false); // Don't wrap at end of line - will do ourselves
matrix.setTextColor(matrix.Color333(7,7,7));
matrix.println("FIST");
matrix.println(" BUMP");
// print each letter with a rainbow color
matrix.setTextColor(matrix.Color333(7,0,0));
matrix.print('C');
matrix.setTextColor(matrix.Color333(7,4,0));
matrix.print('O');
matrix.setTextColor(matrix.Color333(7,7,0));
matrix.print('U');
matrix.setTextColor(matrix.Color333(4,7,0));
matrix.print('N');
matrix.setTextColor(matrix.Color333(0,7,0));
matrix.println('T');
void loop(void)
{
fsr.read();
if (fsr.wasPressed()) {
Serial.print(++counter, DEC);
Serial.println(" steps");
}
matrix.setTextColor(matrix.Color333(0,7,7));
matrix.print("29291");
// whew!
}
void loop() {
// do nothing
}
Firstly, you need to remove the loop() method from setup().
In the void loop() method, making following changes should get things working.
void loop(){
fsr.read();
if( fsr.wasPressed() ){
counter = counter + 1
matrix.setTextColor( matrix.Color333(0,7,7) );
matrix.print( counter )
}
}
EDIT
// testshapes demo for Adafruit RGBmatrixPanel library.
// Demonstrates the drawing abilities of the RGBmatrixPanel library.
// For 32x32 RGB LED matrix:
// http://www.adafruit.com/products/607
// Written by Limor Fried/Ladyada & Phil Burgess/PaintYourDragon
// for Adafruit Industries.
// BSD license, all text above must be included in any redistribution.
#include <Adafruit_GFX.h> // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library
// If your 32x32 matrix has the SINGLE HEADER input,
// use this pinout:
#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
#define OE 9
#define LAT 10
#define A A0
#define B A1
#define C A2
#define D A3
#define PULLUP true //use the AVR's internal pullup resistor
#define INVERT true //low level means fsr pressed
#define DEBOUNCE_TIME 50 //milliseconds
// If your matrix has the DOUBLE HEADER input, use:
//#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
//#define LAT 9
//#define OE 10
//#define A A3
//#define B A2
//#define C A1
//#define D A0
RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);
void setup(){
Serial.begin(9600);
matrix.begin();
// draw a pixel in solid white
matrix.drawPixel(0, 0, matrix.Color333(7, 7, 7));
delay(500);
// fix the screen with green
matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 7, 0));
delay(500);
// draw a box in yellow
matrix.drawRect(0, 0, 32, 32, matrix.Color333(7, 7, 0));
delay(500);
// draw an 'X' in red
matrix.drawLine(0, 0, 31, 31, matrix.Color333(7, 0, 0));
matrix.drawLine(31, 0, 0, 31, matrix.Color333(7, 0, 0));
delay(500);
// draw a blue circle
matrix.drawCircle(10, 10, 10, matrix.Color333(0, 0, 7));
delay(500);
// fill a violet circle
matrix.fillCircle(21, 21, 10, matrix.Color333(7, 0, 7));
delay(500);
// fill the screen with 'black'
matrix.fillScreen(matrix.Color333(0, 0, 0));
// draw some text!
matrix.setCursor(1, 0); // start at top left, with one pixel of spacing
matrix.setTextSize(1); // size 1 == 8 pixels high
matrix.setTextWrap(false); // Don't wrap at end of line - will do ourselves
matrix.setTextColor(matrix.Color333(7,7,7));
matrix.println("FIST");
matrix.println(" BUMP");
// print each letter with a rainbow color
matrix.setTextColor(matrix.Color333(7,0,0));
matrix.print('C');
matrix.setTextColor(matrix.Color333(7,4,0));
matrix.print('O');
matrix.setTextColor(matrix.Color333(7,7,0));
matrix.print('U');
matrix.setTextColor(matrix.Color333(4,7,0));
matrix.print('N');
matrix.setTextColor(matrix.Color333(0,7,0));
matrix.println('T');
}
void loop(){
const int fsr_pin = A4; //connect fsr from this pin to ground
int read fsrAnalogPin = 4;
fsr_pin.read();
if( fsr_pin.wasPressed() ){
counter = counter + 1
matrix.setTextColor( matrix.Color333(0,7,7) );
matrix.print( counter )
}
}
I will also suggest following proper indentation. That will make your code readable. It would be also great if you revise your Arduino prog. concepts.

Arduino pulse train

I will give you a little introduction:
I am working on a water fuel cell of Stanley Meyer. For those who don't know the water fuel cell you can see it here.
For the water fuel cell one has to build a circuit. Here is the diagram:
Right now I am working on the pulse generator (variable) and the pulse gate (variable) to generate this waveform.
So, I want to do this with Arduino timers. I already can generate a "high frequency" pulse generator (1 kHz - 10 kHz, depending on the prescaling at the TCCR2B register) PWM at pin 3 with this code:
pinMode(3, OUTPUT);
pinMode(11, OUTPUT);
TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(WGM22) | _BV(CS21) | _BV(CS20);
OCR2A = 180;
OCR2B = 50;
I can modify the frequency and pulse with:
sensorValue = analogRead(analogInPin);
sensorValue2 = analogRead(analogInPin2);
// Map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 30, 220);
outputValue2 = map(sensorValue2, 0, 1023, 10, 90);
OCR2A = outputValue;
This is working fine.
Now I want to modulate this pulse with another pulse train with "low frequency" (20 Hz to 100 Hz approximately) to act as a pulse gate. I was thinking to use Timer 0 to count and shutdown the signal when it counts some value and activate when arrives at the same value again, like this
TCCR0A = _BV(COM0A0) | _BV(COM0B0) | _BV(WGM01);
TCCR0B = _BV(CS02);
OCR0A = 90;
OCR0B = OCR0A * 0.8;
And compare with the counter
if (TCNT0 <= OCR0A)
TCCR2A ^= (1 << COM2A0);
But it does not work well. Any ideas for this?
These days I have tried to create a wave generator like the one that comes in your question. I can not eliminate the jitter or mismatch that appears, but I can create a wave like that. Try this example and modify it:
#include <TimerOne.h>
const byte CLOCKOUT = 11;
volatile byte counter=0;
void setup() {
Timer1.initialize(15); // Every 15 microseconds change the state
// of the pin in the wave function giving
// a period of 30 microseconds
Timer1.attachInterrupt(Onda);
pinMode(CLOCKOUT, OUTPUT);
digitalWrite(CLOCKOUT, HIGH);
}
void loop() {
if (counter>=29) { // With 29 changes I achieve the amount of pulses I need.
Timer1.stop(); // Here I create the dead time, which must be in HIGH.
PORTB = B00001000;
counter = 0;
delayMicroseconds(50);
Timer1.resume();
}
}
void Onda(){
PORTB ^= B00001000; // Change pin status
counter += 1;
}

Resources