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");
  }
}


Friday, August 19, 2016

43: Linear Motors -- Arduino
revised 8/26/16

I got a stepper working after I bought one with the control interface card. But for my "Dog Treat" system I decided that a linear motor would be most reliable for spitting out dog treats. I bought 2. Here's #1:


#2:

                        top of #2                   under side     control board
                                                        of  #2             for 28B...

Right away, problems. My 28BYJ-40 stepper has 5 wires to the little board. My linear #1 only has 4 wires (blue, yellow,black, red). Will the little interface work? How do I wire it? Ditto for #2 (after I figure out how to solder the tiny contacts).

REVISION:
To get motor #1 to work I bought an "Easy Driver" through Amazon. After the nasty soldering and a couple wrong-headed examples from Googling "arduino stepper..." I got "SparkFun Easy Driver Basic Demo" code & wiring to work.

Motor #2: Soldering to those tiny contacts? First off, I shorted 2 and had to de-solder & start over. Then the same code & Easy Driver worked -- except -- forward and backward are reversed (but then the contacts weren't labeled). Also, #2 spiral gear is "coarser" so a small motor rotation moves much farther. Actually (ignoring soldering), I like #2 better -- the 2 screws will make it easier to connect to something useful.

Also: what's the best way to avoid running the plastic piece past the end of the screw? 

Wednesday, August 10, 2016

42: DHT22 Update of Post 39

Well, my sensor inside a cut-off water bottle failed after a couple days. So I put the next try inside a opaque plastic box that is sealed except for 2 screen-covered 1" holes. But the thing that makes it work (I think) is the tiny fan that blows directly on the DHT22. It still stops working briefly when the humidity reaches 100% but it then recovers after an hour or so. To make this work I had to increase the 5v power to the Photon to 2.4 amps.

I also tried the HIH-4030 sensor in the same environment. Guess what: it stops working at 100% RH, too.

Sunday, June 5, 2016

41: Stepper Motors

I have a mad urge to build an interactive amusement for the left-at-home dogs in my extended family. Yeah, I know other people have-done/are-doing this. So what. Details at http://dicks-raspberry-pi.blogspot.com/, post 99.

Anyway, one feature has to be the ability of the device to spit out doggie treats. I expect the main computer for this will be a Raspberry Pi. And I started stepper programming in Python on the Pi but realized quickly that precise timing is a problem again. Mostly because I want to be able to calibrate the motor position using a photo sensor. Reading such a sensor on the non-analog Pi is slow. So I'll add an Arduino for the motor and the photo sensor chore.

Stepper 28BYJ-48 5V DC + ULN2003 Driver Board
-- don't buy without the driver board --
(otherwise the wiring will drive you nuts)

Here's some C++ I'm currently using (9th try from downloaded sources, I forget which):

int motorPin1 = 8;    // Blue   - 28BYJ48 pin 1
int motorPin2 = 9;    // Pink   - 28BYJ48 pin 2
int motorPin3 = 10;    // Yellow - 28BYJ48 pin 3
int motorPin4 = 11;    // Orange - 28BYJ48 pin 4
                       // Red    - 28BYJ48 pin 5 (VCC), not used
int bits[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};

int photo = A5;

void setup() {
  //declare the motor pins as outputs
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  Serial.begin(9600);
  pinMode(photo, INPUT);
}

char ch;

void loop() {
  // wait for any typed character
  if(!Serial.available()) return;
  ch = Serial.read();
  spr(ch);
  clockwise(128); // not exactly 1 rotation
  delay(1000);
  anticlockwise(128);
  delay(1000);
}

void clockwise(int n) {
  for(int j = 0; j < n; ++j) {
    for(int i = 7; i >= 0; i--) {
      setOutput(i);
      delay(2);
    }
  }
}

void anticlockwise(int n) {
  for(int j = 0; j < n; ++j) {
    for(int i = 0; i < 8; i++) {
      setOutput(i);
      // read photo pin & stop on overrun
      delay(2);
    }
  }
}
void setOutput(int out)
{
  digitalWrite(motorPin1, bitRead(bits[out], 0));
  digitalWrite(motorPin2, bitRead(bits[out], 1));
  digitalWrite(motorPin3, bitRead(bits[out], 2));
  digitalWrite(motorPin4, bitRead(bits[out], 3));
}
==============
To be added to Arduino code: serial command from the Pi.

Question: anyone know how to change the steps per rotation?