This code is broken up into three tabs in the IDE.
First tab (Nokia_LCD) displays stuff to the screen, and dictates the sweep parameters (angle, number of samples/step). As you can tell I just used the demo Nokia sketch and built on it:
/*
Nokia LCD Demo - A demo using the NokiaLCD library for a Nokia LCD with the epson driver.
Created by Thomas Jespersen, July 2009 (Originally Arduino Sketch by Gravitech.us)
Released into the public domain.
*/
// Stepper Driver Info
// Step Clock Pin: 7
// Half/Full Step Pin: 8
// Direction: 9
// Enable: 10
// Reset: 11
#include <NokiaLCD.h>
#include <Stepper.h>
// Nokia LCD Setup settings
#define RED 0x1C
#define GREEN 0xE0
#define BLUE 0x03
#define YELLOW 0xFC
#define MAGENTA 0x1F
#define CYAN 0xE3
#define BLACK 0x00
#define WHITE 0xFF
NokiaLCD nokiaLcd;
const int cellPin = A0;
const int battPin = A1;
const int s1 = 12; // Switch 1 on Nokia LCD board
const int s2 = 13; // Switch 2 on Nokia LCD board
const int step1 = 8;
const int step2 = 9;
const int step3 = 10;
const int step4 = 11;
int sensorValue = 0; // variable to store the value coming from the sensor
int switch1 = 0;
int switch2 = 0;
char text[50];
char pinA0[10];
char pinA1[10];
float fBattVoltage = 0;
int iBattVoltage = 0;
long int runAverage = 0;
long int cellVoltage = 0;
int move;
int midpoint;
long int totalSteps;
int multipleV;
boolean centered;
Stepper myStepper(400, 8,9,10,11,1);
void setup()
{
Serial.begin(57600);
DDRD |= B01111100; // Set SPI pins as output
PORTD |= B01111100; // Set SPI pins HIGH
nokiaLcd.lcd_init();
delay(100);
pinMode(s1, INPUT);
pinMode(s2, INPUT);
pinMode(7, OUTPUT);
analogReference(INTERNAL); // Uses internal 1.1V reference
}
void loop()
{
/* This code reads analog input A0 to determine the solar cell voltage */
//float averageVoltage = 0.0;
runAverage = runningAVG(cellPin,5);
//averageVoltage = (((float)runAverage/1024.0f)*1.1f)*1000.0f; // Convert reading to Voltage
//cellVoltage =(int)averageVoltage;
cellVoltage = (runAverage*1100) / 1024; // Convert reading to Voltage
/* This code reads analog input A1 to determine the battery voltage */
runAverage = runningAVG(battPin,5);
fBattVoltage = (((float)runAverage/1024.0f)*1.1f)/(0.1232f)*1000.0f;
iBattVoltage = (int)fBattVoltage;
/* This will sound an alarm if the battery voltage drops below the cutoff. Be sure to set correct cut-off for battery! */
// 2-cell Lipo cutoff: 6.8 V
// if (iBattVoltage > 6800) digitalWrite(
if (iBattVoltage < 6800)
{
digitalWrite(7,HIGH);
delay(500);
digitalWrite(7,LOW);
delay(500);
}
char battVoltageStr[50];
/* This code updates the screen (Nokia LCD) with the solar cell voltage, and the battery voltage. */
strcpy(text,"Cell Voltage");
nokiaLcd.lcd_draw_text(BLUE, WHITE, 10, 20, text);
sprintf(pinA0,"%i mV ", cellVoltage);
nokiaLcd.lcd_draw_text(RED, WHITE, 10, 37, pinA0);
strcpy(text,"Battery Voltage");
nokiaLcd.lcd_draw_text(BLUE, WHITE, 10, 60, text);
sprintf(battVoltageStr,"%d",iBattVoltage);
sprintf(pinA1,"%c.%c%c V ",battVoltageStr[0],battVoltageStr[1],battVoltageStr[2]);
nokiaLcd.lcd_draw_text(RED, WHITE, 10, 75, pinA1);
switch1 = digitalRead(s1);
switch2 = digitalRead(s2);
char sweepVals;
if (switch1 == LOW) sweep(180,400,50,0); // sweep(angle, steps per revolution, number of times to sample each step, which ADC pin to analyze)
//sweepVals will return [move, totalSteps, midpoint]
if (switch2 == LOW)
{
int center = 0;
if (centered)
{
Serial.println("Already centered, no move.");
}
else if (move < midpoint)
{
center = totalSteps - move - midpoint;
myStepper.step(center);
centered = true;
Serial.print("Distance to center (Loop1): ");
Serial.println(center);
}
else if (move > midpoint)
{
center = move + midpoint - totalSteps;
myStepper.step(-center);
centered = true;
Serial.print("Distance to center (Loop3): ");
Serial.println(-center);
}
}
/* This turns off all of the pins controlling the stepper motor. These are initially off, but
the stepper library leaves certain pins on depending on which step it just took. Waste of power
unless you need some holding capability, but I doubt they intended it to work that way. */
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}
The next tab is runningAVG, it takes an average of however many samples:
int runningAVG(const int sensorPin, const int numReadings)
{
// ADC Smoothing parameters
long int readings[numReadings]; // the readings from the analog input
long int total = 0; // the running total
long int average = 0; // the average
for (int thisReading = 0; thisReading < numReadings; thisReading++) readings[thisReading] = 0; // Resets all contents of "readings" to zero
/* This code performs a running average on the selected pin with number of samples defined by "numReadings"*/
for (int index = 0; index < numReadings; index++)
{
total = total - readings[index]; // subtract the last reading for this index:
Serial.print("Begin ADC.");
readings[index] = analogRead(sensorPin); // read from the sensor:
Serial.print("Finish ADC.");
total = total + readings[index]; // add the reading to the total:
if (index >= numReadings) index = 0; // if we're at the end of the array wrap around to the beginning:
}
average = total / numReadings; // calculate the average:
return average;
}