Arduino Code Examples

This Page is all about my code examples for the projects I make

This is the firmware for controlling the V3.01 Turret grating control mount

#include <LiquidCrystal_I2C.h>
#include <Wire.h>

/* Program 1 REV B

Updated 6/18/2017 @ 11:42:AM
Code concept and project design by David H Haffner Sr.

Stepper program for the 28YBJ-48 (stepper MTR) and ULN2003 driver
This particular stepper motor is 5.625 degrees per step
/64

Speed is controlled by a delay between each step.
The longer the delay the slower the rotation.
That delay value is obtained by reading and analog-to-digital
cover (A0 in this case/10K trimmer POT) which gives a value from 0 to 1023.
The value is divided by 4 and add 10 for a delay
in milliseconds:delay(analogRead(0)/4 +10)

For faster speeds change 10 to say 2.

This is calculated between every step to vary speed while stepping.

I incorporated unsigned long int Val, in order read a little bit more of the AN/Map
A nice feature of unsigned ints: if a val is unsigned, then val / 4 is optimized by
the compiler into a bit shift, much more efficient than the actual division you would
get if val was signed.

Further incorporated a pin array;//read the pushbutton value into a variable
int sensorVal[] = { digitalRead [2][3] };//SW1 pin2 & SW2 pin3
This will illuminat LED’s #10(W) and LED #12(bl) to indicate that the switches
on HIGH. These values are then displayed on the LCD menu on line 3 as a monitor
of the switches values.

The commands below will be compiled into machine code and uploaded
to the microcontroller.

This is in the public domain.

Compiled size 6196 bytes.

*/

#define yellow 7 // M1
#define orange 5 // M2
#define brown 6 // M3
#define blue 4 // M4

#define CW 2 //Sw1 in schematic
#define CCW 3 //Sw2 in schematic

unsigned long int val = (analogRead(‘…’) / 4 + 2);

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address

void setup() {

pinMode(10, OUTPUT);// this is the LED pin for sensor val prgm
pinMode(12, OUTPUT);// this is for LED pin 12 sensor Val prgm
pinMode(CW, INPUT);
pinMode(CCW, INPUT);

digitalWrite(CW, 1); // pull up on
digitalWrite(CCW, 1); // pull up on

pinMode(blue, OUTPUT);
pinMode(brown, OUTPUT);
pinMode(orange, OUTPUT);
pinMode(yellow, OUTPUT);

// all coils off
digitalWrite(blue, 0);
digitalWrite(brown, 0);
digitalWrite(orange, 0);
digitalWrite(yellow, 0);

lcd.begin(20, 4); // initialize the lcd for 20 chars 4 lines, turn on backlight

// ——- Quick 3 blinks of backlight ————-
for (int i = 0; i < 3; i++)
{
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
lcd.backlight(); // finish with backlight on
// set up the LCD’s number of columns and rows:
lcd.begin(20, 4);
// Print a message to the LCD.
lcd.print(“AN/MAP:”);

lcd.setCursor(12, 0);

lcd.print(“0-1023”);

lcd.setCursor(0, 1);
lcd.print(“Direction:”);//CCW or CC

lcd.setCursor(0, 3);
lcd.print(“V3.01 Turret Control”);//4th line for version display

lcd.setCursor(0, 2);
lcd.print(“Val-Pin 2/3:”);

Serial.begin(115200);

}

 

void loop() {

if (!digitalRead(CW)) {
forward(10);
all_coils_off();
}

 

if (!digitalRead(CCW)) {

reverse(10);
all_coils_off();
}

//read the pushbutton value into a variable
int sensorVal[] = { digitalRead [2][3] };//SW pin2 and pin3
{
lcd.setCursor(12, 2);//print out the value of the pushbutton
lcd.print(‘sensorVal’);
}
// Keep in mind the pullup means the pushbutton’s
// logic is inverted. It goes HIGH when it’s open,
// and LOW when it’s pressed. Turn on pin 13 when the
// button’s pressed, and off when it’s not:
if (sensorVal == HIGH) {
digitalWrite(10, LOW);//LED pin
} else {
digitalWrite(10, HIGH);//LED pin
}

if (sensorVal == HIGH) {
digitalWrite(12, LOW);//LED pin
} else {
digitalWrite(12, HIGH);//LED pin
}

} // end loop

 

void all_coils_off(void) {

digitalWrite(blue, 0);
digitalWrite(brown, 0);
digitalWrite(orange, 0);
digitalWrite(yellow, 0);

}

 

void reverse(int i) {
{
lcd.setCursor(10, 1);
lcd.print(“<<CCW”);
}
while (1) {

digitalWrite(blue, 1);
digitalWrite(brown, 0);
digitalWrite(orange, 1);
digitalWrite(yellow, 0);
delay(analogRead(‘…’) / 4 + 2);
i–;
if (i < 1) break;

 

digitalWrite(blue, 0);
digitalWrite(brown, 1);
digitalWrite(orange, 1);
digitalWrite(yellow, 0);
delay(analogRead(‘…’) / 4 + 2);
i–;
if (i < 1) break;
{
lcd.setCursor(7, 0);
lcd.print(analogRead(‘…’) / 4 + 2);
}
digitalWrite(blue, 0);
digitalWrite(brown, 1);
digitalWrite(orange, 0);
digitalWrite(yellow, 1);
delay(analogRead(‘…’) / 4 + 2);
i–;
if (i < 1) break;

digitalWrite(blue, 1);
digitalWrite(brown, 0);
digitalWrite(orange, 0);
digitalWrite(yellow, 1);
delay(analogRead(‘…’) / 4 + 2);
i–;
if (i < 1) break;
}

}

 

void forward(int i) {
{
lcd.setCursor(10, 1);
lcd.print(“CW>>>”);
}
while (1) {

digitalWrite(blue, 1);
digitalWrite(brown, 0);
digitalWrite(orange, 0);
digitalWrite(yellow, 1);
delay(analogRead(‘…’) / 4 + 2);
i–;
if (i < 1) break;

digitalWrite(blue, 0);
digitalWrite(brown, 1);
digitalWrite(orange, 0);
digitalWrite(yellow, 1);
delay(analogRead(‘…’) / 4 + 2);
i–;
if (i < 1) break;

digitalWrite(blue, 0);
digitalWrite(brown, 1);
digitalWrite(orange, 1);
digitalWrite(yellow, 0);
delay(analogRead(‘…’) / 4 + 2);
i–;
if (i < 1) break;

digitalWrite(blue, 1);
digitalWrite(brown, 0);
digitalWrite(orange, 1);
digitalWrite(yellow, 0);
delay(analogRead(‘…’) / 4 + 2);
i–;
if (i < 1) break;
}
}

 

This is the Code for running the TCD1304DG 16 Bit ADC for the CCD detector Project

#include <util/delay_basic.h>

#define RD (1<<2)
#define CNVST (1<<3)
#define BYTESWAP (1<<4)
#define ICG (1<<5)
#define SH (1<<6)
#define MCLK (1<<7)

// Full frame, including dark pixels
// and dead pixels.
#define PIXEL_COUNT 3691

// Ports and pins
#define CLOCKS PORTD
#define CLOCKP PIND
#define CLOCKS_DDR DDRD
#define DATA_PINS PINC
#define DATA_PORT PORTC
#define DATA_DDR DDRC

// 10mS exposure time.
#define EXPOSURE_TIME 10

// Initial clock state.
uint8_t clocks0 = (RD + CNVST + ICG);

// 16-bit pixel buffer
uint16_t pixBuf[PIXEL_COUNT];

char cmdBuffer[16];
int cmdIndex;
int exposureTime = EXPOSURE_TIME;
int cmdRecvd = 0;

/*
* readLine() Reads all pixels into a buffer.
*/

void readLine() {
// Get an 8-bit pointer to the 16-bit buffer.
uint8_t *buf = (uint8_t *) pixBuf;
int x = 0;
uint8_t scratch = 0;

// Disable interrupts or the timer will get us.
cli();

// Synchronize with MCLK and
// set ICG low and SH high.
scratch = CLOCKS;
scratch &= ~ICG;
scratch |= SH;
while(!(CLOCKP & MCLK));
while((CLOCKP & MCLK));
TCNT2 = 0;
_delay_loop_1(1);
__asm__(“nop”);
__asm__(“nop”);
__asm__(“nop”);
__asm__(“nop”);
__asm__(“nop”);
CLOCKS = scratch;

// Wait the remainder of 4uS @ 20MHz.
_delay_loop_1(22);
__asm__(“nop”);
__asm__(“nop”);

// Set SH low.
CLOCKS ^= SH;

// Wait the reaminder of 4uS.
_delay_loop_1(23);

// Start the readout loop at the first pixel.
CLOCKS |= (RD + CNVST + ICG + BYTESWAP + SH);
__asm__(“nop”);

do {
// Wait a minimum of 250nS for acquisition.
_delay_loop_1(2);

// Start the conversion.
CLOCKS &= ~CNVST;
CLOCKS |= CNVST;

// Wait a minimum of 1uS for conversion.
_delay_loop_1(4);

// Read the low byte of the result.
CLOCKS &= ~RD;
_delay_loop_1(4);

*buf++ = DATA_PINS;

// Setup and read the high byte.
CLOCKS &= ~(BYTESWAP);
_delay_loop_1(4);

*buf++ = DATA_PINS;

// Set the clocks back to idle state
CLOCKS |= (RD + BYTESWAP);

// Toggle SH for the next pixel.
CLOCKS ^= SH;

} while (++x < PIXEL_COUNT);

sei();
}

/*
* clearLine() Clears the CCD.
*/

void clearLine() {

int x = 0;

// Set ICG low.
CLOCKS &= ~ICG;
CLOCKS |= SH;
_delay_loop_1(14);

// Set SH low.
CLOCKS ^= SH;
_delay_loop_1(10);

// Reset the timer so the edges line up.
TCNT2 = 0;

CLOCKS |= (RD + CNVST + ICG + BYTESWAP + MCLK);

do {
CLOCKS ^= SH;
_delay_loop_1(10);

} while (++x < PIXEL_COUNT);

}

/*
* sendLine() Send the line of pixels to the user.
*/
void sendLine() {
uint16_t x;

for (x = 0; x < PIXEL_COUNT; ++x) {
Serial.print(x);
Serial.print(“,”);
Serial.print(pixBuf[x]);
Serial.print(“\n”);
}
}

/*
* setup()
* Set the data port to input.
* Set the clock port to output.
* Start timer2 generating the Mclk signal
*/

void setup() {
DDRB |= 0x01;
PORTB &= ~1;
delay(10);
CLOCKS_DDR = 0xff;
CLOCKS = 0; //clocks0;
DATA_DDR = 0x0;
Serial.begin(115200);
PORTB |= 0x01;
// Setup timer2 to generate an 888kHz frequency on D10
TCCR2A = (0 << COM2A1) | (1 << COM2A0) | (1 << WGM21) | (0 << WGM20);
TCCR2B = (0 << WGM22) | (1 << CS20);

OCR2A = 8;
TCNT2 = 0;
delay(10);
}

/*
* loop()
* Read the CCD continuously.
* Upload to user on switch press.
*/

void loop() {

int x;
char ch;

// If we got a command last time, execute it now.
if (cmdRecvd) {
if (cmdBuffer[0] == ‘r’) {

// Send the readout to the host.
sendLine();
} else if (cmdBuffer[0] == ‘e’) {
delay(10);
Serial.write(cmdBuffer);
// Set the exposure time.
sscanf(cmdBuffer + 1, “%d”, &exposureTime);
if (exposureTime > 1000) exposureTime = 1000;
if (exposureTime < 1) exposureTime = 1;
}

// Get ready for the next command.
memset(cmdBuffer, 0, sizeof(cmdBuffer));
cmdIndex = 0;
cmdRecvd = 0;
}
// Clear the CCD.
clearLine();

// Integrate.
delay(exposureTime);

// Read it for real.
readLine();

// See if the host is talking to us.
if (Serial.available()) {
ch = Serial.read();

// If char is linefeed, it is end of command.
if (ch == 0x0a) {
cmdBuffer[cmdIndex++] = ‘\0’;
cmdRecvd = 1;

// Otherwise it is a command character.
} else {
cmdBuffer[cmdIndex++] = ch;
cmdRecvd = 0;
}
}
}

 

This is the Fast 8 Bit code for the TCD1304DG CCD detector utilizing the ADC0820CCN/NOPB

#include <util/delay_basic.h>

// Debug point pin 16
#define DBG 0x04

// ADC RD signal pin 17
#define RD 0x08

// ADC write signal pin 18
#define WR 0x10

// CCD Shift Gate pin 19
#define SH 0x20

// CCD Integration Clear Gate pin 20
#define ICG 0x40

// CCD Master clock pin 21
#define MCLK 0x80

// CCD and ADC clocks
#define CLOCK PORTD

// ADC data
#define ADATA PINC

uint8_t buffer[3694];
char cmdBuffer[16];
int cmdIndex;
int exposureTime = 10;

void setup()
{
// Initialize the clocks.
DDRD |= (WR | SH | ICG | MCLK | RD | DBG); // Set the clock lines to outputs
CLOCK |= ICG; // Set the integration clear gate high.
CLOCK |= (RD | WR); // Set the ADC wr line high.

// Setup the ADC data port.
DDRC = 0;
// Enable the serial port.
Serial.begin(115200);

// Setup timer2 to generate a 470kHz frequency on pin 21
TCCR2A = + (0 << COM2A1) | (1 << COM2A0) | (1 << WGM21) | (0 << WGM20);
TCCR2B = (0 << WGM22) | (1 << CS20);
OCR2A = 16;
TCNT2 = 1;
}

void readCCD(void)
{
int x;
uint8_t dummy;

// Clear the CCD shift register and
// dump the electrons into it.
CLOCK |= DBG;
CLOCK &= ~ICG;
_delay_loop_1(12);
CLOCK |= SH;
delayMicroseconds(5);
CLOCK &= ~SH;
delayMicroseconds(10);
CLOCK |= ICG;
delayMicroseconds(1);

for (x = 0; x < 3694; x++)
{
// Shift out one pixel and
// digitize it.
CLOCK |= SH;

// ADC write.
CLOCK &= ~WR;
delayMicroseconds(1);
CLOCK |= WR;

// ADC convert.
delayMicroseconds(2);

// ADC read.
CLOCK &= ~RD;
delayMicroseconds(1);
buffer[x] = ADATA;
CLOCK |= RD;

// Eat up a few cycles for timing.
asm(“nop”);
asm(“nop”);
asm(“nop”);
asm(“nop”);

CLOCK &= ~SH;
delayMicroseconds(4);
}
CLOCK &= ~DBG;
}

void sendData(void)
{
int x;

for (x = 30; x < 3678; ++x)
{
Serial.print(x – 30);
Serial.print(“,”);
Serial.print(buffer[x]);
Serial.print(“\n”);
}
}

int cmdRecvd = 0;

void loop()
{
int x;
char ch;

if (cmdRecvd) {
if (cmdBuffer[0] == ‘r’)
{
sendData();
}
else if (cmdBuffer[0] == ‘e’)
{
sscanf(cmdBuffer+1,”%d”, &exposureTime);
if (exposureTime > 1000) exposureTime = 1000;
}
memset(cmdBuffer, 0, sizeof(cmdBuffer));
cmdIndex = 0;
cmdRecvd = 0;
}
delay(exposureTime);
readCCD();
if (Serial.available())
{
ch = Serial.read();
if (ch == 0x0a) {
cmdBuffer[cmdIndex++] = ‘\0’;
cmdRecvd = 1;
} else {
cmdBuffer[cmdIndex++] = ch;
cmdRecvd = 0;
}
}
}

V3.01 Turret Grating Control Firmaware (REV B)

#include <LiquidCrystal_I2C.h>
#include <Wire.h>

/* Program 1

Stepper motor demo for the 28YBJ-48 (stepper MTR) and ULN2003 driver
This particular stepper motor is 5.625 degrees per step
/64

Speed is controlled by a delay between each step.
The longer the delay the slower the rotation.
That delay value is obtained by reading and analog-to-digital
cover (A0 in this case/10K trimmer POT) which gives a value from 0 to 1023.
The value is divided by 4 and add 10 for a delay
in milliseconds:delay(analogRead(0)/4 +10)
For faster speeds change 10 to say 2.
This is calculated between every step to vary speed while stepping.

The commands below will be compiled into machine code and uploaded
to the microcontroller.

Compiled size 1896 bytes.

*/

#define yellow 7 // M1
#define orange 5 // M2
#define brown 6 // M3
#define black 4 // M4

#define CW 2 //Sw1 in schematic
#define CCW 3 //Sw2 in schematic

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address

void setup() {

 

pinMode(CW, INPUT);
pinMode(CCW, INPUT);

digitalWrite(CW, 1); // pull up on
digitalWrite(CCW, 1); // pull up on

pinMode(black, OUTPUT);
pinMode(brown, OUTPUT);
pinMode(orange, OUTPUT);
pinMode(yellow, OUTPUT);

// all coils off
digitalWrite(black, 0);
digitalWrite(brown, 0);
digitalWrite(orange, 0);
digitalWrite(yellow, 0);

lcd.begin(20, 4); // initialize the lcd for 20 chars 4 lines, turn on backlight

// ——- Quick 3 blinks of backlight ————-
for (int i = 0; i < 3; i++)
{
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
lcd.backlight(); // finish with backlight on
// set up the LCD’s number of columns and rows:
lcd.begin(20, 4);
// Print a message to the LCD.
lcd.print(“AN/MAP:”);

lcd.setCursor(12, 0);

lcd.print(“0-1023”);

lcd.setCursor(0, 1);
lcd.print(“Direction:”);//CCW or CC

lcd.setCursor(0, 2);
lcd.print(“V3.01 Turret Control”);//4th line for version display

Serial.begin(115200);

}

 

void loop() {

if (!digitalRead(CW)) {

forward(10);
all_coils_off();
}

 

if (!digitalRead(CCW)) {

reverse(10);
all_coils_off();
}

} // end loop

 

void all_coils_off(void) {

digitalWrite(black, 0);
digitalWrite(brown, 0);
digitalWrite(orange, 0);
digitalWrite(yellow, 0);

}

 

void reverse(int i) {
{
lcd.setCursor(10, 1);
lcd.print(“<<CCW”);
}
while (1) {

digitalWrite(black, 1);
digitalWrite(brown, 0);
digitalWrite(orange, 1);
digitalWrite(yellow, 0);
delay(analogRead(0) / 4 + 2);
i–;
if (i < 1) break;

 

digitalWrite(black, 0);
digitalWrite(brown, 1);
digitalWrite(orange, 1);
digitalWrite(yellow, 0);
delay(analogRead(0) / 4 + 2);
i–;
if (i < 1) break;
{
lcd.setCursor(7, 0);
lcd.print(analogRead(0) / 4 + 2);
}
digitalWrite(black, 0);
digitalWrite(brown, 1);
digitalWrite(orange, 0);
digitalWrite(yellow, 1);
delay(analogRead(0) / 4 + 2);
i–;
if (i < 1) break;

digitalWrite(black, 1);
digitalWrite(brown, 0);
digitalWrite(orange, 0);
digitalWrite(yellow, 1);
delay(analogRead(0) / 4 + 2);
i–;
if (i < 1) break;
}

}

 

void forward(int i) {
{
lcd.setCursor(10, 1);
lcd.print(“CW>>>”);
}
while (1) {

digitalWrite(black, 1);
digitalWrite(brown, 0);
digitalWrite(orange, 0);
digitalWrite(yellow, 1);
delay(analogRead(0) / 4 + 2);
i–;
if (i < 1) break;

digitalWrite(black, 0);
digitalWrite(brown, 1);
digitalWrite(orange, 0);
digitalWrite(yellow, 1);
delay(analogRead(0) / 4 + 2);
i–;
if (i < 1) break;

digitalWrite(black, 0);
digitalWrite(brown, 1);
digitalWrite(orange, 1);
digitalWrite(yellow, 0);
delay(analogRead(0) / 4 + 2);
i–;
if (i < 1) break;

digitalWrite(black, 1);
digitalWrite(brown, 0);
digitalWrite(orange, 1);
digitalWrite(yellow, 0);
delay(analogRead(0) / 4 + 2);
i–;
if (i < 1) break;
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s