code (final) dec 8th

// Variables for calculating PERIOD and SEX pointers
//float speedPointer();
//#include <avr/pgmspace.h>
int encoder[106];
//PROGMEM prog_uchar encoder[106];
//prog_uchar encoder[106] PROGMEM;
unsigned char lastEncoder0Pos;
int displayRead;

unsigned char encoder0Pos;
int timePeriod=60;
int passedDays=15;
int cycleCount;
int gear20=18;
int gear24=96;
int delayRate;
int pointerPos=0;
//int sexPoint=0;
//int reading[107];
int initialSteps;
int delayMillis;
int savedMillis;
int ellapsedMillis;
int displayEncoder;
//int lastEncoder0Pos;
int lastChange;

//Variables for communicating to Processing
int firstByte; // value for Period
int secondByte; // value for Initial Date
int serialInArray[3]; // array for storing 3 bytes as they arrive from processing
int serialCount = 0; // for counting the number of bytes received

// ENCODER Variables
#define encoder0PinA 2
#define encoder0PinB 3
//volatile int encoder0Pos = 0;

// Stepper Motor Variables
int counter=0;
#include <Stepper.h>
//Motor Steps for Motor 1
#define motor1Steps 24 // change this depending on the number of steps
// Steps fo motor 2
#define motor2Steps 20 // per revolution of your motor
// Pins 4 to 7 for Motor 1
#define motorPin1 4
#define motorPin2 5
#define motorPin3 6
#define motorPin4 7
//Pins 8-11 for Motor 2
#define motorPin5 8
#define motorPin6 9
#define motorPin7 10
#define motorPin8 11
// initialize of the Stepper library:
Stepper myStepper(motor1Steps, motorPin1,motorPin2,motorPin3,motorPin4);
Stepper myStepper2(motor2Steps,motorPin5,motorPin6,motorPin7,motorPin8);

void setup() {
// set the motor speed at 60 RPMS:
myStepper.setSpeed(10);
myStepper2.setSpeed(60);

// Initialize the Serial port:
Serial.begin(9600);

//SETUP FOR ENCODER
pinMode(encoder0PinA, INPUT);
digitalWrite(encoder0PinA, HIGH); // turn on pullup resistor
pinMode(encoder0PinB, INPUT);
digitalWrite(encoder0PinB, HIGH); // turn on pullup resistor
attachInterrupt(0, doEncoder, CHANGE); // encoder pin on interrupt 0 - pin 2
Serial.begin (9600);
Serial.println("start"); // a personal quirk
savedMillis=millis();
initialSteps=106.67*passedDays/timePeriod;
myStepper2.step(int(initialSteps));

Serial.println(initialSteps);
}

void loop() {

/*
if (Serial.available() > 0) {
// read the most recent byte (which will be from 0 to 255)
if(Serial.read()==65){
timePeriod = Serial.read();
passedDays = Serial.read();
}
}
*/
/*
serialInArray[serialCount] = Serial.read(); // read a byte sent by processing
serialCount++; // increment number of bytes received
if (serialCount > 2 ) { // when 3 bytes received
firstByte = serialInArray[1]; // get value for PERIOD
secondByte = serialInArray[2]; // get value for INITIAL DATE
serialCount = 0;
}
*/
//delayMillis=1000*gear20*timePeriod/(gear24*motor2Steps);
delayMillis=9.375*timePeriod;
// delayMillis=0;
// Serial.println(delayMillis);

//+" "+delayMillis+" "+savedMillis);

ellapsedMillis=millis()-savedMillis;
if(ellapsedMillis>=delayMillis){
// Serial.println(ellapsedMillis);
myStepper2.step(1);
savedMillis=millis();
pointerPos+=1;

//encoder[pointerPos]=encoder0Pos;
//encoder0Pos=lastEncoder0Pos;
Serial.println(pointerPos);
// displayEncoder = pgm_read_byte_near(lastEncoder0Pos);
// Serial.println(displayEncoder);

///////// STEPPER1.STEP()

// getPeriod(savedMillis);

}
if (pointerPos == 106){
cycleCount+=1;
pointerPos=0;
if (cycleCount==3){
myStepper2.step(3);
}
Serial.println("cycleCount");
}
// Serial.println(cycleCount);

for (int i=0; i<pointerPos; i++){
encoder[i]=displayRead;
Serial.println(displayRead);
if (displayRead>1 && i==pointerPos){
if (displayRead != encoder0Pos)
myStepper.step(1);
}
}
}

void doEncoder(){
encoder[pointerPos]=encoder0Pos;
if (digitalRead(encoder0PinA) == HIGH) { // found a low-to-high on channel A
if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way
// encoder is turning
encoder0Pos = encoder0Pos - 1; // CCW
}
else {
encoder0Pos = encoder0Pos + 1; // CW
}
}
else // found a high-to-low on channel A
{
if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way
// encoder is turning
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}

if(encoder0Pos>64){
encoder0Pos=1;
}
if(encoder0Pos<1){
encoder0Pos=64;
}

/*
if (encoder0Pos != lastEncoder0Pos) {
// save the last change in the encoder:
lastEncoder0Pos = encoder0Pos;
// note the last time the encoder changed:
lastChange = millis();
}

if (millis() - lastChange >= 1) {
// save the reading
encoder[pointerPos]=encoder0Pos;
// Serial.println(lastEncoder0Pos);
//prog_uchar encoder[pointerPos]=lastEncoder0Pos;
}

*/

// Serial.println (encoder0Pos, DEC); // debug - remember to comment out

// before final program run
}

 

December 2007
M T W T F S S
« Nov    
 12
3456789
10111213141516
17181920212223
24252627282930
31