Hi everyone! For some reason, at the end of my long long day I'm struggling to get an output from my Arduino. I purely want pin 15 to go high, which powers a transistor powering a small stepper motor controller.
All is well if I 5v directly onto it, but I can't get the pin to output, not sure what I'm doing wrong.
Please advise if my code is doing something basic wrong, could be me, struggle with basic things!
Thanks guys.
/*
Rotary Encoder Demo
rot-encode-demo.ino
Demonstrates operation of Rotary Encoder
Displays results on Serial Monitor
DroneBot Workshop 2019
https://dronebotworkshop.com
*/
// Include the AccelStepper library and LCD:
#include <AccelStepper.h>
#include <LiquidCrystal_I2C.h>
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
// Create a new instance of the AccelStepper class and LCD i2c:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C address 0x27, 16 column and 2 rows
// Rotary Encoder Inputs
// LED Outputs
#define ledCW 13
#define ledCCW 9
#define inputCLK 4
#define inputDT 5
int buttontest = 16;
int ledPin = 15; // LED connected to digital pin 13
int relayPin = 15; // Pin for relay control (which is normally open)
int inPin = 33; // pushbutton connected to digital pin 7
int val = 1; // variable to store the read value
int relay = 0;
int counter = 0;
int currentStateCLK;
int previousStateCLK;
int movement;
String encdir ="";
void setup() {
lcd.init(); // initialize the lcd
lcd.backlight();
// Set the maximum speed and acceleration:
stepper.setMaxSpeed(1000);
stepper.setAcceleration(500);
// Set encoder pins as inputs
pinMode (inputCLK,INPUT);
pinMode (inputDT,INPUT);
// Set LED pins as outputs
pinMode (ledCW,OUTPUT);
pinMode (ledCCW,OUTPUT);
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
pinMode(inPin, INPUT); // sets the digital pin 7 as input
pinMode(relayPin, OUTPUT); // sets the digital pin 7 as input
// Setup Serial Monitor
Serial.begin (9600);
// Read the initial state of inputCLK
// Assign to previousStateCLK variable
previousStateCLK = digitalRead(inputCLK);
lcd.clear();
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Stick'orme'eter"); // print message at (0, 0)
lcd.setCursor(0, 1);
lcd.print("Version 45.2145");
lcd.setCursor(0, 3);
lcd.print("Ernest Jackson 2022");
// Set red LED next to ribbon, to flash, to ensure this operates during start up opeartion
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
delay(3000);
lcd.clear();
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("LETS GOOOOOOO BOI!!!"); // print message at (0, 0)
lcd.setCursor(0, 2); // move cursor to (0, 0)
lcd.print("B===D - - -"); // print message at (0, 0)
delay(1500);
lcd.clear();
lcd.print("Waiting for input..");
Serial.print("Val = ");
Serial.println(val);
}
void loop() {
Serial.println("Loop phase started.");
delay(50);
// End of Current Read Script------
// Read the current state of inputCLK
Serial.println("digitalhome reading.");
Serial.print("Val = ");
Serial.println(val);
if (val == HIGH) {
Serial.println("if statement ");
Serial.println("System ready, waiting.");
lcd.clear();
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("System ready.."); // print message at (0, 0)
lcd.setCursor(0, 2); // move cursor to (0, 0)
lcd.print("..waiting for input."); // print message at (0, 0)
Serial.print("Val = ");
Serial.println(val);
delay(1000);
}
if (val == LOW) {
Serial.println("Home procedure activated");
// Run to target position with set speed and acceleration/deceleration:
Serial.println("Stepper moving...");
lcd.clear();
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Home calibration"); // print message at (0, 0)
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("procedure activating"); // print message at (0, 0)
Serial.print("Val = ");
Serial.println(val);
delay(1000);
digitalWrite(ledPin, LOW);
delay(250);
digitalWrite(ledPin, HIGH);
delay(250);
stepper.moveTo(200);
stepper.runToPosition();
stepper.moveTo(0);
stepper.runToPosition();
Serial.println("...home operation completed.");
lcd.clear();
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("...home operation completed."); // print message at (0, 0)
delay(500);
Serial.println("Set stepper to move to home.");
lcd.clear();
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Stepper moving to home"); // print message at (0, 0)
stepper.moveTo(1500);
stepper.runToPosition();
delay(1000);
lcd.setCursor(0, 0);
lcd.print("Limit switch reached.");
lcd.setCursor(0, 2);
lcd.print("Plug 12v feed now or");
lcd.setCursor(0, 3);
lcd.print("relay turn power.");
digitalWrite(relayPin, HIGH);
delay(1000);
stepper.moveTo(100);
stepper.runToPosition();
delay(4000);
Serial.println("Datum reached.");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Home program");
lcd.setCursor(0, 1);
lcd.print("completed.");
}
if (buttontest == HIGH) {
Serial.println("Motion Downwards");
stepper.moveTo(0);
stepper.runToPosition();
Serial.println("Stepper moving downwards..");
stepper.runToPosition();
Serial.println("Stepper motion complete.");
}
// Serial.print("Direction: ");
// Serial.print(encdir);
// Serial.print(" -- Value: ");
// Serial.println(counter);
val = digitalRead(inPin); // read the input pin
if (val == LOW) {
digitalWrite(ledPin, HIGH); // sets the LED to the button's value
}
else
{
digitalWrite(ledPin, LOW); // sets the LED to the button's value
}
Serial.print(val);
}