ball_read: 32u4 -> arduino pro mini
parent
47385e9f19
commit
bb7677eda5
|
@ -19,45 +19,29 @@
|
||||||
S16 27 PD7
|
S16 27 PD7
|
||||||
loop cycle duration: 3.2 millis
|
loop cycle duration: 3.2 millis
|
||||||
**/
|
**/
|
||||||
|
#define S10 ((PINB & 1))
|
||||||
|
#define S9 ((PINB & 2) >> 1)
|
||||||
|
#define S8 ((PINB & 4) >> 2)
|
||||||
|
#define S7 ((PINB & 8) >> 3)
|
||||||
|
#define S6 ((PINB & 16) >> 4)
|
||||||
|
#define S5 ((PINB & 32) >> 5)
|
||||||
|
|
||||||
#define S5 ((PINE & 64 ) >> 6)
|
#define S4 ((PINC & 1))
|
||||||
|
#define S3 ((PINC & 2) >> 1)
|
||||||
|
#define S2 ((PINC & 4) >> 2)
|
||||||
|
#define S1 ((PINC & 8) >> 3)
|
||||||
|
|
||||||
#define S11 ((PINC & 128 ) >> 7)
|
#define S16 ((PIND & 4) >> 2)
|
||||||
#define S12 ((PINC & 64 ) >> 6)
|
#define S15 ((PIND & 8) >> 3)
|
||||||
|
#define S14 ((PIND & 16) >> 4)
|
||||||
#define S13 ((PINB & 64 ) >> 6)
|
#define S13 ((PIND & 32) >> 5)
|
||||||
#define S14 ((PINB & 32 ) >> 5)
|
#define S12 ((PIND & 64) >> 6)
|
||||||
#define S15 ((PINB & 16) >> 4)
|
#define S11 ((PIND & 128) >> 7)
|
||||||
|
|
||||||
#define S1 ((PIND & 64 ) >> 6)
|
|
||||||
#define S2 ((PIND & 16 ) >> 4)
|
|
||||||
#define S3 ((PIND & 2 ) >> 1)
|
|
||||||
#define S4 (PIND & 1 )
|
|
||||||
#define S16 ((PIND & 128) >> 7)
|
|
||||||
|
|
||||||
#define S6 ((PINF & 2 ) >> 1)
|
|
||||||
#define S7 ((PINF & 16 ) >> 4)
|
|
||||||
#define S8 ((PINF & 32 ) >> 5)
|
|
||||||
#define S9 ((PINF & 64 ) >> 6)
|
|
||||||
#define S10 ((PINF & 128 ) >> 7)
|
|
||||||
|
|
||||||
#define NCYCLES 250
|
#define NCYCLES 250
|
||||||
#define BROKEN 230
|
#define BROKEN 230
|
||||||
#define TOO_LOW 45
|
#define TOO_LOW 45
|
||||||
|
|
||||||
#define LED1ON (PORTD = PORTD | 0b00100000)
|
|
||||||
#define LED1OFF (PORTD = PORTD & 0b11011111)
|
|
||||||
|
|
||||||
#define LED2ON (PORTF = PORTF | 0b00000001)
|
|
||||||
#define LED2OFF (PORTF = PORTF & 0b11111110)
|
|
||||||
|
|
||||||
#define LED3ON (PORTB = PORTB | 0b10000000)
|
|
||||||
#define LED3OFF (PORTB = PORTB & 0b01111111)
|
|
||||||
|
|
||||||
#define LED4ON (PORTB = PORTB | 0b00000001)
|
|
||||||
#define LED4OFF (PORTB = PORTB & 0b11111110)
|
|
||||||
|
|
||||||
|
|
||||||
int counter[16];
|
int counter[16];
|
||||||
int distance;
|
int distance;
|
||||||
int nmax = 0;
|
int nmax = 0;
|
||||||
|
@ -82,7 +66,7 @@ unsigned long t = 0;
|
||||||
void setup() {
|
void setup() {
|
||||||
delay(1000);
|
delay(1000);
|
||||||
|
|
||||||
Serial1.begin(57600);
|
Serial.begin(57600);
|
||||||
|
|
||||||
|
|
||||||
/*For now replace pinMode with writes to the direction register.
|
/*For now replace pinMode with writes to the direction register.
|
||||||
|
@ -90,13 +74,7 @@ void setup() {
|
||||||
but this needs further investigation*/
|
but this needs further investigation*/
|
||||||
|
|
||||||
//Set the LEDs as outputs, keep the rest as input by default
|
//Set the LEDs as outputs, keep the rest as input by default
|
||||||
|
pinMode(A4, OUTPUT);
|
||||||
//LED3(PB7) and LED4 (PB0)
|
|
||||||
DDRB |= 0b10000001;
|
|
||||||
//LED2 (PF0)
|
|
||||||
DDRF |= 0b00000001;
|
|
||||||
//LED1 (PD5)
|
|
||||||
DDRD |= 0b00100000;
|
|
||||||
|
|
||||||
/*pinMode(26, INPUT); //S1
|
/*pinMode(26, INPUT); //S1
|
||||||
pinMode(25, INPUT); //S2
|
pinMode(25, INPUT); //S2
|
||||||
|
@ -125,7 +103,6 @@ void loop() {
|
||||||
readBallInterpolation();
|
readBallInterpolation();
|
||||||
//printCounter();
|
//printCounter();
|
||||||
sendDataInterpolation();
|
sendDataInterpolation();
|
||||||
readFromTeensy();
|
|
||||||
//test();
|
//test();
|
||||||
//delay(100);
|
//delay(100);
|
||||||
}
|
}
|
||||||
|
@ -172,6 +149,7 @@ void readBallInterpolation() {
|
||||||
//dist = hypot(x, y);
|
//dist = hypot(x, y);
|
||||||
|
|
||||||
nmax = 0;
|
nmax = 0;
|
||||||
|
|
||||||
//saves max value and sensor
|
//saves max value and sensor
|
||||||
for (int i = 0; i < 16; i++) {
|
for (int i = 0; i < 16; i++) {
|
||||||
if (counter[i] > nmax) {
|
if (counter[i] > nmax) {
|
||||||
|
@ -184,20 +162,20 @@ void readBallInterpolation() {
|
||||||
|
|
||||||
//turn led on
|
//turn led on
|
||||||
if (distance == 0) {
|
if (distance == 0) {
|
||||||
LED1OFF;
|
digitalWrite(A4, LOW);
|
||||||
} else {
|
} else {
|
||||||
LED1ON;
|
digitalWrite(A4, HIGH);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendDataInterpolation() {
|
void sendDataInterpolation() {
|
||||||
if(sending){
|
if(sending){
|
||||||
sendAngle = ((byte) (angle / 2)) & 0b11111110;
|
sendAngle = ((byte) (angle / 2)) & 0b11111110;
|
||||||
Serial1.write(sendAngle);
|
Serial.write(sendAngle);
|
||||||
}else{
|
}else{
|
||||||
sendDistance = map(distance, 0, NCYCLES, 254, 0);
|
sendDistance = NCYCLES - distance;
|
||||||
sendDistance = sendDistance |= 0b00000001;
|
sendDistance = sendDistance |= 0b00000001;
|
||||||
Serial1.write(sendDistance);
|
Serial.write(sendDistance);
|
||||||
}
|
}
|
||||||
sending = !sending;
|
sending = !sending;
|
||||||
}
|
}
|
||||||
|
@ -205,41 +183,41 @@ void sendDataInterpolation() {
|
||||||
void test() {
|
void test() {
|
||||||
readBallInterpolation();
|
readBallInterpolation();
|
||||||
|
|
||||||
Serial1.println("===========");
|
Serial.println("===========");
|
||||||
Serial1.print(S1);
|
Serial.print(S1);
|
||||||
Serial1.print(" | ");
|
Serial.print(" | ");
|
||||||
Serial1.print(S2);
|
Serial.print(S2);
|
||||||
Serial1.print(" | ");
|
Serial.print(" | ");
|
||||||
Serial1.print(S3);
|
Serial.print(S3);
|
||||||
Serial1.print(" | ");
|
Serial.print(" | ");
|
||||||
Serial1.print(S4);
|
Serial.print(S4);
|
||||||
Serial1.print(" | ");
|
Serial.print(" | ");
|
||||||
Serial1.print(S5);
|
Serial.print(S5);
|
||||||
Serial1.print(" | ");
|
Serial.print(" | ");
|
||||||
Serial1.print(S6);
|
Serial.print(S6);
|
||||||
Serial1.print(" | ");
|
Serial.print(" | ");
|
||||||
Serial1.print(S7);
|
Serial.print(S7);
|
||||||
Serial1.print(" | ");
|
Serial.print(" | ");
|
||||||
Serial1.print(S8);
|
Serial.print(S8);
|
||||||
Serial1.print(" | ");
|
Serial.print(" | ");
|
||||||
Serial1.print(S9);
|
Serial.print(S9);
|
||||||
Serial1.print(" | ");
|
Serial.print(" | ");
|
||||||
Serial1.print(S10);
|
Serial.print(S10);
|
||||||
Serial1.print(" | ");
|
Serial.print(" | ");
|
||||||
Serial1.print(S11);
|
Serial.print(S11);
|
||||||
Serial1.print(" | ");
|
Serial.print(" | ");
|
||||||
Serial1.print(S12);
|
Serial.print(S12);
|
||||||
Serial1.print(" | ");
|
Serial.print(" | ");
|
||||||
Serial1.print(S13);
|
Serial.print(S13);
|
||||||
Serial1.print(" | ");
|
Serial.print(" | ");
|
||||||
Serial1.print(S14);
|
Serial.print(S14);
|
||||||
Serial1.print(" | ");
|
Serial.print(" | ");
|
||||||
Serial1.print(S15);
|
Serial.print(S15);
|
||||||
Serial1.print(" | ");
|
Serial.print(" | ");
|
||||||
Serial1.print(S16);
|
Serial.print(S16);
|
||||||
Serial1.print(" --- ");
|
Serial.print(" --- ");
|
||||||
Serial1.println(sensor);
|
Serial.println(sensor);
|
||||||
Serial1.println("===========");
|
Serial.println("===========");
|
||||||
delay(100);
|
delay(100);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -247,26 +225,14 @@ void test() {
|
||||||
|
|
||||||
void printCounter() {
|
void printCounter() {
|
||||||
for (int i = 0; i < 16; i++) {
|
for (int i = 0; i < 16; i++) {
|
||||||
Serial1.print(counter[i]);
|
Serial.print(counter[i]);
|
||||||
Serial1.print(" | ");
|
Serial.print(" | ");
|
||||||
}
|
}
|
||||||
Serial1.print("\t\t| Angle: " );
|
Serial.print("\t\t| Angle: " );
|
||||||
Serial1.print(angle);
|
Serial.print(angle);
|
||||||
Serial1.print("||| Distance: " );
|
Serial.print("||| Distance: " );
|
||||||
Serial1.print(distance);
|
Serial.print(distance);
|
||||||
|
|
||||||
Serial1.println();
|
Serial.println();
|
||||||
delay(100);
|
delay(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
void readFromTeensy(){
|
|
||||||
while(Serial1.available()){
|
|
||||||
byte b = Serial1.read();
|
|
||||||
if(b & 0b00000001 == 1) LED2ON;
|
|
||||||
else LED2OFF;
|
|
||||||
if((b & 0b00000010) >> 1 == 1) LED3ON;
|
|
||||||
else LED3OFF;
|
|
||||||
if((b & 0b00000100) >> 2 == 1) LED4ON;
|
|
||||||
else LED4OFF;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue