EBB Notes 3

From Noisebridge
Revision as of 15:29, 25 February 2011 by Rbelknap (talk | contribs) (added category tag)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

I (Rachel) moved the MAXsonar sensor circuit to my own boarduino, successfully. I copied Lamont's shift register/PWM circuit to my own boarduino, again (eventually) successfully. So far this is mere sideways motion... but I have made actual progress in understanding the PWM code, so should soon be able to modify it to include the sensor. Yay tiny progress!

Current unsuccessful code that attempts to read the MAXsonar EZ1's serial out:

#include <ctype.h>

#define bit9600Delay 84  
#define halfBit9600Delay 42
#define bit4800Delay 188 
#define halfBit4800Delay 94 
#define PACKET_LENGTH 5

byte rx = 2;
byte tx = 3;
byte SWval[PACKET_LENGTH];

void setup() {
  pinMode(rx,INPUT);
  pinMode(tx,OUTPUT);
  digitalWrite(tx,HIGH);
  digitalWrite(13,HIGH); //turn on debugging LED
//  SWprint('h');  //debugging hello
//  SWprint('i');
//  SWprint(10); //carriage return
  Serial.begin(9600);
}

void loop() {
    Serial.println("begin: ");
    
    // wait for the start of a packet
    int allLow = 1;
    while(!allLow) {
      allLow = 1;
      for(int i = 0; i < 10*10; ++i) {
         delayMicroseconds(bit9600Delay);
         if(digitalRead(rx) == HIGH) {
           allLow = 0;
         }
      }
    }
    Serial.println("p");
    
    // read a packet
    for(int i = 0; i < PACKET_LENGTH; ++i) {
      SWval[i] = SWreadNi(); 
    }
    Serial.println("r");

    // print the packet
    // TESTING shows that this bit is BROKEN.
    for(int i = 0; i < PACKET_LENGTH; ++i) {
      for(int j = 0; j < 8; ++i) {
        Serial.print( (SWval[i] >> j) & 1 );
      }
      Serial.print(" ");
    }
    Serial.println();
    Serial.println();
}

void SWprint(int data) {
  byte mask;
  //startbit
  digitalWrite(tx,LOW);
  delayMicroseconds(bit9600Delay);
  for (mask = 0x01; mask>0; mask <<= 1) {
    if (data & mask){ // choose bit
     digitalWrite(tx,HIGH); // send 1
    }
    else{
     digitalWrite(tx,LOW); // send 0
    }
    delayMicroseconds(bit9600Delay);
  }
  //stop bit
  digitalWrite(tx, HIGH);
  delayMicroseconds(bit9600Delay);
}

int SWreadNi() {
  byte val = 0;
  while (digitalRead(rx) == LOW);
  //wait for start bit
  if (digitalRead(rx) == HIGH) {
    delayMicroseconds(halfBit9600Delay);
    for (int offset = 0; offset < 8; offset++) {
     delayMicroseconds(bit9600Delay);
     val |= (digitalRead(rx) << offset);
    }
    //wait for stop bit + extra
//    delayMicroseconds(bit9600Delay); 
//    delayMicroseconds(bit9600Delay);
    return ~val;
  }
}

int SWread() {
  byte val = 0;
  while (digitalRead(rx));
  //wait for start bit
  if (digitalRead(rx) == LOW) {
    delayMicroseconds(halfBit9600Delay);
    for (int offset = 0; offset < 8; offset++) {
     delayMicroseconds(bit9600Delay);
     val |= digitalRead(rx) << offset;
    }
    //wait for stop bit + extra
    delayMicroseconds(bit9600Delay); 
    delayMicroseconds(bit9600Delay);
    return val;
  }
}