If function, basic but struggling

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

     
   } 

Which "pin 15"?

1 Like

Which Arduino?

Relay pin. Looks like i copied over a working function and lect them both there. My error.

Sadly didnt work when singilar remaimed

Mega mate

Why is buttontest set to 16 and you are testing it for HIGH in loop()?
How is your pushbutton wired? Do you have a pullup or pulldown resistor?
Why are you testing val before you read the pushbutton?

Thanks for your reply.

Looking for HIGH functions as i have two inout push buttons for which i want to move the motor.

The push button for the homing function for which im trying to resolve actually grounds. Which is why im looking for a LOW in that instance.

I have a pullup resistornto 5v. Im able to get a successful high / low output on the serial. Its as if it just doesnt want to fire up pin 15 as an output. Thinking i was struggling identifying out to set a digital weite to output.

The val setup was purely a copy of another program. No real reason for its test.

You can do without the resistor if you use INPUT_PULLUP.

I'm going to assume you meant:

if (digitalRead(buttontest) == HIGH) {

Instead of:

if (buttontest == HIGH) {

Because the latter will never be true.

If the relay output is a problem then what happens if you just run the following sketch?

const int relayPin = 15;

void setup() 
{
  pinMode(relayPin, OUTPUT);
  Serial.begin (9600);
}

void loop() 
{
  Serial.println("Set relay pin to LOW");
  digitalWrite(relayPin, LOW);
  delay(1000);
  Serial.println("Set relay pin to HIGH");
  digitalWrite(relayPin, HIGH);  
  delay(1000);
}

When I build something I always write individual sketches to test each hardware component. That way when I write my final code if it doesn't work I can be confident it is not a hardware/wiring issue.

Ill try this first thing tomorrow. I usually check i can get the hardware working but for this part i thought it would be so simple wouldnt be required.

Will try your code first thing and feedback. Thank you mate

Sadly didn't even get a pin output, might have to order a new Arduino

Absolute school boy error.

Had wired to analog 15, changed code to A15 and boom, it worked.

Thanks to everyone who supported.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.