Crit1 Development: Stepper Motor Progress

I have 2 alternatives to power the turning plant- a DC motor or a stepper motor. I have run through a test of a DC motor (attached to a fan) and 2 tests for a stepper motor. I was inclined to try to use the stepper motor, but neither of the stepper motor tests have worked. It appears that the power supply module I’m using (which is supposed to be compatible with the driver and stepper motor I’m using) is failing to provide power to the driver and the stepper motor. Not clear yet where the failure is happening, although the dim LED light on the power supply module suggests that the 9V battery power supply I am using is not adequate. I will need to test another power supply to see if I can get the stepper motor to work. Since the DC motor with fan seemed to have varying success depending on the speed, I tried two stepper motor tests just in case a difference in the speed would make the stepper motor move.

UPDATE 2/28: With a new power supply (wall plug), the stepper motor code is working. Next step is to make a sketch that combines the stepper motor and the light sensor TEMT6000.

Stepper Motor Test 1 Sketch:

#include <Stepper.h>
int stepsPerRevolution=2048; //see motor spec sheet
int motSpeed=10;
int dt=500;
Stepper stepmot(stepsPerRevolution, 8,10,9,11);//see motor and driver specs

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  stepmot.setSpeed(motSpeed);

}

void loop() {
  // put your main code here, to run repeatedly:
  stepmot.step(stepsPerRevolution);
  delay(dt);
  stepmot.step(-stepsPerRevolution);
  delay(dt);


}

Stepper Motor Test 2 Sketch:

#include <Stepper.h>
const float STEPS_PER_REV=32; //see motor spec sheet
const float GEAR_RED = 64;
const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;
int stepsReq;
int motSpeed=10;
int dt=500;
 
Stepper steppermotor(STEPS_PER_REV, 8,10,9,11);//see motor and driver specs

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);


}

void loop() {
  // put your main code here, to run repeatedly:
  steppermotor.setSpeed(1);
  stepsReq=4;
  steppermotor.step(stepsReq);
  delay(2000);

  stepsReq = STEPS_PER_OUT_REV / 2;
  steppermotor.setSpeed(100);
  steppermotor.step(stepsReq);
  delay(1000);


}

 

Crit1 Development: Experiment with Fan Motor and Light Sensor TEMT6000

With the TEMT6000 light sensor, I can turn on a motor based on input to the light sensor, similar to the one I had with the DHT-22.

With the TEMT6000, I used an analog write (instead of the digital write with the DHT-22) and the data arrives in float form. Several lines of code convert the reading into a percent. Data smoothing and millis () set up are the same as previous DHT-22 code.

#define lightPin A0 //Ambient light sensor reading 

//data smoothing
const int numReadings = 5;
float ligReadings [numReadings];
int ligReadIndex = 0;
float ligTotal = 0;
float ligAverage = 0;
int inputPin = lightPin; //put sensor pin here
//detecting sensor change
int last;
//response to sensor change

//for fan and motor
int speedPin = 5;
int dir1 = 4;
int dir2 = 3;
int mSpeed = 0;
unsigned long lastReadTime = 0;

void setup() {
  // put your setup code here, to run once:
  //pins for fan and motot
  pinMode(speedPin, OUTPUT);
  pinMode(dir1, OUTPUT);
  pinMode(dir2, OUTPUT);
  pinMode(lightPin, INPUT);

  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    ligReadings[thisReading] = 0;
  }
  Serial.begin(9600);
  lastReadTime = millis();
}

void loop() {
  //delay(1000);

  if ((millis() - lastReadTime) >= 5000UL) {
    Serial.println(lastReadTime);
    Serial.println(millis());
    lastReadTime = millis();
    //light sensor read
    float light = analogRead(lightPin);
    float light_ratio = light / 1023.0;
    float light_percent = light_ratio * 100;

    ligTotal = ligTotal - ligReadings[ligReadIndex];
    ligReadings[ligReadIndex] = light_percent;
    ligTotal = ligTotal + ligReadings [ligReadIndex];
    ligReadIndex = ligReadIndex + 1;
    if (ligReadIndex >= numReadings) {
      ligReadIndex = 0;
    }
    ligAverage = ligTotal / numReadings;
    Serial.println(light_percent);
    Serial.println(ligAverage);

  }



  if (ligAverage > 25)
  {
    digitalWrite(dir1, HIGH);
    digitalWrite(dir2, LOW);
    analogWrite(speedPin, 225);
  }else
  {
    digitalWrite(dir1,HIGH);
    digitalWrite(dir2,LOW);
    analogWrite(speedPin, 0);
  }
}

Next, I plan to use the sensor to turn another motor (the stepper motor, which I think is a better fit for turning the plant turntable).

Crit1 Development: Experiment with Button and p5js

I ran into challenges with the previous DHT22 sensor to p5js assignment. I performed another test run using input from a button. With this, I was able to connect the p5.js web editor to the arduino, and have an image on the screen of the web editor change when the button input changed.

Arduino sketch below:

const int buttonPin = 2;
int buttonValue = 0; 

void setup() {
  // put your setup code here, to run once:
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
  buttonValue = digitalRead(buttonPin);
  Serial.println(buttonValue);
  delay(10);

}

p5js sketch:

let serial; 
let newData = "waiting for data";

function setup() {
  createCanvas(windowWidth, windowHeight);
  serial = new p5.SerialPort();
  serial.list();
  serial.open('COM5');
  serial.on('connected', serverConnected);
  serial.on('list',gotList);
  serial.on('data',gotData);
  serial.on('error',gotError);
  serial.on('open',gotOpen);
  
}

function serverConnected() {
  print("Connected to Server");
}

function gotList(thelist) {
  print("List of Serial Ports:");
  
  for (let i=0; i < thelist.length; i++) {
    print(i + " " + thelist[i]);
    
  }
}

function gotOpen() {
  print("Serial Port is Open");
}

function gotClose() {
  print("Serial Port is Closed");
  newData = "Serial Port is Closed";
}

function gotError(theerror) {
  print(theerror);
}

function gotData() {
  let currentString = serial.readLine();
  trim(currentString);
  if (!currentString) return;
  console.log(currentString);
  newData = currentString;
}

function draw() {
  background(245,245,245);
  fill(50,50,200);
  text(newData,10,10);
  
  
  if (newData == 0) {
    rectMode(CENTER);
    rect(width/2,height/2,100,100);
  } else {
    ellipse(width/2,height/2,200,200);
    
  }
  
}

Next, for the Crit1 project, I want to be able to record or represent the actions of the plant turntable with p5js.

Resources and Demos: