Hi guys,
I am trying to read in encoder data using an Arduino Mega 2560. I was using digitalRead() and some nested if loops to count the encoder pulses, but my data was sporadic and unreliable. My count per rotation would change with how fast my motors were rotating! Not good. I spoke to a friend very skilled in embedded systems and he told me there was no way a high-level function like digitalRead() could keep up with my encoder, especially considering I am reading in data from six encoders. He recommended I move all the encoder signals to two ports and then use a low-level Port Manipulation line of code to grab all the data.
I put the encoder signal wires on Port C, then wrote a very simple code. It turned a motor (motor six) on and spinning, used DDRC to turn Port C into all inputs and then used PINC to grab all that port data. That's it. But when we tried to port PINC to serial to make sure our data looked good, all we saw were zeros.
We double checked that the two encoder signal pins we had wired to PORT C were oscillating like they were supposed to, and they were - jumping up and down from 0 to 1 as the motor spun. We double checked that motor six's signal pins were on PORTC and they were. But we couldn't seem to grab the data using the PINC command.
I'm pretty sure we were making some ridiculously simple error that we just couldn't spot. Any idea what that error might be? The code is below. The majority of it is variable initialization; the tiny bit of port manipulation is at the very bottom. Thanks in advance for any help.
// setup ports for input; leave bits 6&7 in write... ?
//PWM pins 2,3,4,5,6,7
//Encoder pins 13,14,15,16,17,18
#include <Encoder.h>
///verified
int motor2speed = 13;
int motor1speed = 11;
int motor3speed = 12;
int motor5speed = 10;
int motor4speed = 9;
int motor6speed = 8;
int motor1dir = 38;
int motor2dir = 35;
int motor3dir = 36;
int motor4dir = 39;
int motor6dir = 40;
int motor5dir = 41;
int hefmotor6 = 2;
int hefmotor5 = 3;
int hefmotor4 = 4;
int hefmotor3 = 5;
int hefmotor2 = 6;
int hefmotor1 = 7;
int enc1 = 26;
int enc12 = 27;
int enc2 = 22;
int enc22 = 23;
int enc3 = 24;
int enc32 = 25;
int enc4 = 28;
int enc42 = 29;
int enc5 = 32;
int enc52 = 33;
int enc6 = 30;
int enc62 = 31;
int enc6pos = 0;
int enc6last = LOW;
int n6 = LOW;
int x6 = 0;
int y6 = LOW;
int enc4pos = 0;
int enc4last = LOW;
int n4 = LOW;
int x4 = 0;
int y4 = LOW;
int enc2pos = 0;
int enc2last = LOW;
int n2 = LOW;
int x2 = 0;
int y2 = LOW;
int enc1pos = 0;
int enc1last = LOW;
int n1 = LOW;
int enc3pos = 0;
int enc3last = LOW;
int n3 = LOW;
int enc5pos = 0;
int enc5last = LOW;
int n5 = LOW;
//*/
// define shit
int sensor = 0;
int lastp1 = PINC;
// int lastp2 = B00000000
int p1 = B00000000;
// int p2 = B00000000
int dp;
void setup(){
Serial.begin(9600);
///*
//Motor PWM pins. Must be set to OUTPUT before analogwriting
pinMode(motor6dir, OUTPUT);
pinMode(motor4dir, OUTPUT);
pinMode(motor5dir, OUTPUT);
pinMode(motor1dir, OUTPUT);
pinMode(motor3dir, OUTPUT);
pinMode(motor2dir, OUTPUT);
pinMode(motor1speed, OUTPUT);
pinMode(motor2speed, OUTPUT);
pinMode(motor3speed, OUTPUT);
pinMode(motor4speed, OUTPUT);
pinMode(motor5speed, OUTPUT);
pinMode(motor6speed, OUTPUT);
pinMode(49, OUTPUT);
pinMode(48, OUTPUT);
pinMode(42, OUTPUT);
pinMode(43, OUTPUT);
pinMode(hefmotor1, INPUT); //HEF motor 6
pinMode(hefmotor2, INPUT);
pinMode(hefmotor3, INPUT);
pinMode(hefmotor4, INPUT);
pinMode(hefmotor5, INPUT);
pinMode(hefmotor6, INPUT);
pinMode(enc6, INPUT);
pinMode(enc62, INPUT);
pinMode(enc5, INPUT);
pinMode(enc52, INPUT);
pinMode(enc4, INPUT);
pinMode(enc42, INPUT);
pinMode(enc3, INPUT);
pinMode(enc32, INPUT);
pinMode(enc2, INPUT);
pinMode(enc22, INPUT);
pinMode(enc1, INPUT);
pinMode(enc12, INPUT);
DDRC = B00000000;
}
void loop() {
digitalWrite(motor6dir, HIGH); //sets motor six's direction
analogWrite(motor6speed, 122); //sets motor six's speed
byte damnport = PINC;
//dp = lastp1 ^ p1;
//if (dp & 0x11000000 > 0){
//sensor++;
//}
Serial.println(PINC, BIN);
//lastp1 = p1;
}