Thursday, September 22, 2016

47: Did You Know?

—That your Arduino sketch can re-execute the setup function:

void setup() {
  . . .
  Serial.begin(9600);
  . . .
}

void loop() {
  // Some code
  . . .

}

So, if there is no serial connection when the Arduino powers up then the code will proceed to the loop. However, say you later connect to it with Coolterm (or equilalent command) then the setup will be executed again (for Serial.begin). I think it causes a hardware RESET.

Note that while a Particle Photon is Arduino-like, setup does not get re-executed in the above circumstance.

Probably doesn't matter, but it might.

Friday, September 9, 2016

46: Linear Stepper Motor -- More
-- revised 9/15/16 --

Below is a very messy circuit drawing for my Arduino setup. Should also work with Particle Photon or Raspberry Pi. It uses external 5V to power both the relay switch and the motor. The reason for the relay is that the steppers draw power constantly. So in my program when I want to move the linear screw I turn the power ON, delay(100), operate the stepper, delay(100) and turn the relay OFF. When you power a stepper off the rotor can move freely (no brakes). But with the linear screw that won't happen (its own friction). My cheap stepper has about a 3" movement. Even with counting steps, I will use a door/window sensor to recalibrate the initial position. I plan to use this to dispense dog treats. See post 41.

Revised: I bought a second EasyDriver and motor. They look exactly like the above. After the irritating soldering adventure I hooked it up to an Arduino Nano and loaded the same script that I used with the first pair. With the first set, 350 steps move the length of the spiral screw; the new one takes over 1500. No clue as to why.

Thursday, September 8, 2016

45: Some Obvious info about Opto-isolated Relays

For isolated wiring:
- external 5v to Relay JD-VCC pin (the usual label) and GND
- digital Arduino pins to Relay IN1, etc.
- Arduino 5v or 3.3v to the Relay VCC (next to IN pins)

Isolation comes from NOT connecting GND from the Arduino.

Most switches are "normally open". This would not be my first choice.


See the relay photo.

Wiring A: 
Switch is initially OFF.
digitalWrite(pin, LOW) turns it ON.

Wiring B:
Switch initially ON (even with no power input).
digitalWrite(pin, HIGH) turns it ON.

Wiring B is appealing because HIGH = ON but having even a momentary initial ON is irksome.

So what I do is use Wiring A with the following #define's:

#define SW_ON   LOW     // digitalWrite(pin, SW_ON) -- turn ON
#define SW_OFF  HIGH    // digitalWrite(pin, SW_OFF) -- turn OFF

Otherwise, chaos!

P.S.: Essentially the same goes for Particle Photon and Raspberry Pi.

Saturday, September 3, 2016

44: Read line from USB

I'm not fond of C++ so try to pretend it's just C. So the supplied readline() doesn't fit. Doing a readline that worked for me took a few tries. So, I'll share it. (I'm sure it could be better)

#define spr(x) Serial.print(x)
#define spl(x) Serial.println(x)

char Buffer[80];

int readline(char *buffer, int len)
{
  int pos = 0;
  int tries = 0;
  char ch;

  while(tries < 200) { // arb. time out
    if(pos >= len) return -1;
    ch = Serial.read();
    if(ch > 0) {
      switch (ch) {
      case '\n': // Ignore new-lines
        break;
      case '\r': // Return on CR
        return pos;
      default:
        if (pos < len-1) {
          Buffer[pos++] = ch;
          Buffer[pos] = 0;
        }
      }
    } else { // no input ready
      delay(50);
    }
    ++tries;
  }
  // No end of line has been found, so return -1.
  return -1;
}

void setup() {
  Serial.begin(9600);
  delay(3000);
  Serial.println("Read line");

}

void loop() {
  int ct;
  if (readline(Buffer, 80) > 0) {
    spr("L:");spl(Buffer);
  } else {
    spl("timed out");
  }
}