Stepper motor simple code help

Hi I need help. I'm a beginner with Arduino nano. Trying to make a simple project for a couple months and am a bit lost in it. I have a nano connected to Bluetooth (tx to Rx, Rx to tx pins) and a 4wire stepper motor. I can control it without the Bluetooth but can't seem to figure out the code with Bluetooth. I want to be able to control it through 2 buttons on an android app made in MIT app inventor. If I press one button, it makes the motor turn half a turn 180 degrees, if I press the other button it makes the motor do a full 360 degree turn and stops(degrees don't matter as I can adjust those with steps). I have the app, it connects to Bluetooth on nano but can't figure out the code on the Arduino side. Ive coded the app so when I press button 1, it sends '1', if I press the other it sends '2' through bluetooth. My code is a combination of other people's code but doesn't work. Here's the code I have so far that doesn't work.
All help greatly appreciated!

Updated code


#include <Stepper.h>
#include<SoftwareSerial.h>

const int stepsPerRevolution1 = 200;  
const int stepsPerRevolution2 = 400;

char Incoming_value = 0;
 
boolean didMyOneTimeAction = false;

Stepper myStepper1(stepsPerRevolution1, 3,10,8,11);
Stepper myStepper2(stepsPerRevolution2, 3,10,8,11);

void setup() {
Serial.begin(38400);  
myStepper1.setSpeed(80);
myStepper2.setSpeed(80);
}

void loop() {
 if (didMyOneTimeAction == false)
  {
  if(Serial.available() > 0 ) { 
    Incoming_value = Serial.read();
    Serial.print (Incoming_value);
    Serial.print ("\n");


  if(Incoming_value == 1);
      myStepper1.step(stepsPerRevolution1);  
  }

else if(Incoming_value == 2);
    myStepper2.step(stepsPerRevolution2);
  didMyOneTimeAction = true;
  }  }

Did you forget something here?

Don't you just hate it when that happens?

Hello silly777
Run some tutorials for the hardware selected.
If you are happy with the results of the tutorials you can merge these to your project.
Have a nice day and enjoy coding in C++.

Thanks for the advice. That's how I've managed to get it to run. I'm struggling with the Bluetooth and the option of 2 buttons. I've updated the code tonight and put it in here too. Maybe I'm getting closer but not sure.

It confuses things when you modify the original post rather than post new code. Previous comments then fail to make sense or be relevant to the revised code.

You are making several syntax errors

if(Incoming_value == 1);
      myStepper1.step(stepsPerRevolution1);  
  }

else if(Incoming_value == 2);

Replace the ; after the conditional with a { and set your values to characters rather than integers if that is what you are sending.

if(Incoming_value == '1') {
      myStepper1.step(stepsPerRevolution1);  
  }

else if(Incoming_value == '2') {

Thank you for your help. Will play with it. Ive left the post the same, only put in updated code, though perhaps should have put in it a reply. Thanks again.

Then it’s not the same. You guess correctly: new code, new post.

a7

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