Start and Stop Stepper Motor using Serial Interface - arduino

Here's my code for starting a Stepper motor and trying to stop the motor:
#include <BasicStepperDriver.h>
#define DIR 34
#define STEP 36
#define ENBL 30
BasicStepperDriver stepper1(10000, DIR, STEP, ENBL);
BasicStepperDriver stepper2(10000, DIR, STEP, ENBL);
bool enableMotor1 = false;
bool enableMotor2 = false;
void rotate1() {
Serial.println("Rotate start 1");
stepper1.rotate(360);
Serial.println("Rotate end 1");
}
void rotate2() {
Serial.println("Rotate start 2");
stepper2.rotate(360);
Serial.println("Rotate end 2");
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
stepper1.enable();
stepper1.setMicrostep(1);
stepper1.setRPM(50);
stepper2.enable();
stepper2.setMicrostep(1);
stepper2.setRPM(50);
}
void loop() {
if(enableMotor1 == true) {
rotate1();
} else if(enableMotor2 == true) {
rotate2();
}
if (Serial.available ()) {
String first = Serial.readStringUntil(',');
Serial.read();
String second = Serial.readStringUntil(',');
if(first == "start"){
if(second == "1") {
Serial.println("Starting motor 1");
enableMotor1 = true;
}
if(second == "2") {
Serial.println("Starting motor 2");
enableMotor2 = true;
}
} else if(first == "stop") {
if(second == "1") {
Serial.println("Stopping motor 1");
enableMotor1 = false;
stepper1.disable();
}
if(second == "2") {
enableMotor2 = false;
Serial.println("Stopping motor 1");
stepper2.disable();
}
}
}
}
The problem with this code is that it is not possible to stop the motor through Serial interface. The goal is to have the Stepper motor running until the stop command is entered in the serial monitor. What could be wrong in my code?
This image shows that we can start the motor but can't be stopped:

Related

How to execute a function just once in the void loop?

Simply, I want to make a counter to execute a subroutine if digital read gets a toggle value just thefirst time. But the result shows that the "aFunction" executes more than once (repeatedly).
Code:
int lastState = 1;
void setup() {
Serial.begin(115200);
}
void loop() {
int currentState = digitalRead(D5);
if (currentState == 1) {
if (currentState = !lastState) {
aFunction();
}
lastState = !lastState;
}
Serial.println("Still running loop");
delay(2000);
}
void aFunction() {
Serial.println("in a function");
}
This is the final result:
bool executed = false;
void setup()
{
Serial.begin(115200);
}
void loop()
{
int currentState = digitalRead(D5);
if (currentState == 1 && executed == false)
{
aFunction();
executed = true;
}
if (currentState == 0 && executed == true)
{
aFunction();
executed = false;
}
Serial.println("Still running loop");
delay(2000);
}
void aFunction()
{
Serial.println("in a function");
}
By keeping track of execution of function and checking it along with currentState variable will help you in achieving your goal.
Your code with this change will look like:
bool executed = false;
void setup() {
Serial.begin(115200);
}
void loop() {
int currentState = digitalRead(D5);
if (currentState == 1 && executed == false) {
executed = true;
aFunction();
}
Serial.println("Still running loop");
delay(2000);
}
void aFunction() {
Serial.println("in a function");
}
You need a variable to signal it's been run once:
int lastState = 1;
int runOnce = 0;
void setup() {
Serial.begin(115200);
}
void loop() {
int currentState = digitalRead(D5);
if (currentState == 1) {
if (currentState = !lastState) {
if (runOnce == 0)
aFunction();
}
lastState = !lastState;
}
Serial.println("Still running loop");
delay(2000);
}
void aFunction() {
Serial.println("in a function");
runOnce = 1;
}
So now your aFunction() sets the runOnce flag to 1 and it won't ever run again until the device is reset due to the if statement inside loop().
Your problem lies right here:
if (currentState == 1) {
if (currentState = !lastState) {
aFunction();
}
lastState = !lastState;
}
This will run indefinitely until currentState /= 1. You are having problems because you're not exactly understanding how the code runs in runtime. The computer is reading this repeatedly, and its reading if currentState==1 to execute the bracket.
The next issue is your nested if statement, it's not using a comparison operator. You have an = sign, but you should have an == operator to compare currentstate to laststate.
Essentially, if you want it to run once, you need to make sure the function changes the variable currentState to something other than 1, then if you want this function to execute again at some other point during runtime, change currentState back 1 for this function to run again, where again it'll change currentState to something other than 1.
I suggest instead of using an integer for currentState to use a boolean true or false. If true, execute, change to false at the end - since its changed to false it wont run again until its true again.

Arduino output problems

I am currently attempting to make a calculator, and this is the first part of the equation where the user will enter a number using an IR remote.
Currently my problem is when I am outputting the numbers I have entered into the array as strings (so I can combine them later on) it outputs odd characters.
This is my code so far:
#include <boarddefs.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <ir_Lego_PF_BitStreamEncoder.h>
int RECV_PIN = 6;
IRrecv irrecv(RECV_PIN);
decode_results results;
int part1digits = 0;
String part1[16];
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
if (part1digits <= 15) {
while (!irrecv.decode(&results)) {
/* DO NOTHING WHEN NO IR SIGNAL IS RECEIVED
This WHILE loop will run continuously, doing nothing
until there IS a signal received
*/
}
// if IR signals is received, then do this
getCode();
part1digits++;
} else {
for (int i = 0; i <= 15; i++) {
Serial.print(part1[part1digits]);
}
// The while loop below is to 'stop' the program
while(1){}
}
}
void getCode() {
if (results.value == 16738455) {
part1[part1digits] = "0";
Serial.println(part1[part1digits]);
}
else if (results.value == 16724175) {
part1[part1digits] = "1";
Serial.println(part1[part1digits]);
}
else if (results.value == 16718055) {
part1[part1digits] = "2";
Serial.println(part1[part1digits]);
}
else if (results.value == 16743045) {
part1[part1digits] = "3";
Serial.println(part1[part1digits]);
}
else if (results.value == 16716015) {
part1[part1digits] = "4";
Serial.println(part1[part1digits]);
}
else if (results.value == 16726215) {
part1[part1digits] = "5";
Serial.println(part1[part1digits]);
}
else if (results.value == 16734885) {
part1[part1digits] = "6";
Serial.println(part1[part1digits]);
}
else if (results.value == 16728765) {
part1[part1digits] = "7";
Serial.println(part1[part1digits]);
}
else if (results.value == 16730805) {
part1[part1digits] = "8";
Serial.println(part1[part1digits]);
}
else if (results.value == 16732845) {
part1[part1digits] = "9";
Serial.println(part1[part1digits]);
}
// Serial.println(results.value, HEX);
delay(250); // Delay, so you don't send 3 signals back to back
// while the button is depressed.
irrecv.resume(); // Receive the next value
}
A small snippet of output I am getting after I have entered in all my values are:
ôïÿ9ÝKÏÞóõx;ÿoקÿ^Øè«ÙÝ™ë]^ �ßå¿n'Þ
This happens because you selected the wrong baud rate on the serial monitor. The code says 9600, so make sure that the serial monitor is selected at that baud rate.
Check your baudrate again, try to change baudrate between 9600, 38400 or 57600

Arduino: If/Else issues

Situation: I have a webpage that has two buttons on it. One sends to the PubNub channel a JSON string of {"lightRight":"1"} and one sends {"lightRight":"0"}, regardless of what I do in the Arduino Sketch I cannot make it into the "on" portion of the void light function.
I know from the serial output of the arduino that I am receiving communication from the web page. If I flip my boolean values to off then I am able to get the "LED HIGH" output but then no longer can I get the "led low".
The code I am trying to adapt can be found here.
https://gist.github.com/ianjennings/ada8fb1a91a486a0c73e
It is very possible I have hacked out to much code from the examples that is why I am including it, but running the stock code I cannot seem to get this to work.
I am not a developer, so I am very sorry if I do not call things as their proper terms. I will learn from correction.
#include <PubNub.h>
#include <SPI.h>
#include <EthernetV2_0.h>
#include <string.h>
#include <Servo.h>
byte mac[] = { MAC was here };
byte gateway[] = { Gate was here };
byte subnet[] = { Sub was here };
IPAddress ip(IP was here);
char pubkey[] = "Key was here";
char subkey[] = "Key was here";
char channel[] = "Channel was here";
int lightRight = 5;
int lightRoom = 6;
int lightGarage = 7;
int i;
EthernetClient *client;
#define W5200_CS 3
#define SDCARD_CS 4
void setup()
{
pinMode(SDCARD_CS,OUTPUT);
digitalWrite(SDCARD_CS,HIGH);
Serial.begin(9600);
Serial.println("Serial set up");
while (!Ethernet.begin(mac)) {
Serial.println("Ethernet setup error");
blink(1000, 999999);
delay(1000);
}
Serial.println("Ethernet set up");
PubNub.begin(pubkey, subkey);
Serial.println("PubNub set up");
pinMode(lightRight, OUTPUT);
pinMode(lightRoom, OUTPUT);
pinMode(lightGarage, OUTPUT);
// blink(100, 5);
reset();
}
//void flash(int ledPin)
//{
// for (int i = 0; i < 3; i++) {
// Serial.println("flash");
// digitalWrite(ledPin, HIGH);
// delay(100);
// digitalWrite(ledPin, LOW);
// delay(100);
// }
//}
void loop()
{
Ethernet.maintain();
PubSubClient *client;
Serial.println("waiting for a message (subscribe)");
client = PubNub.subscribe(channel);
if (!client) {
Serial.println("subscription error");
return;
}
String messages[10];
boolean inside_command = false;
int num_commands = 0;
String message = "";
char c;
while (client->wait_for_data()) {
c = client->read();
if(inside_command && c != '"') {
messages[num_commands] += c;
}
if(c == '"') {
if(inside_command) {
num_commands = num_commands + 1;
inside_command = false;
} else {
inside_command = true;
}
}
message += c;
}
client->stop();
for (i = 0; i < num_commands; i++){
int colonIndex = messages[i].indexOf(':');
String subject = messages[i].substring(0, colonIndex);
String valueString = messages[i].substring(colonIndex + 1, messages[i].length());
boolean value = false;
if(valueString == "1") {
value = true;
}
if(subject == "lightRight") {
light(lightRight, value);
}
if(subject == "lightRoom") {
light(lightRoom, value);
}
if(subject == "lightGarage") {
light(lightGarage, value);
}
if(subject == "blink") {
blink(100, valueString.toInt());
}
Serial.println(subject);
Serial.println(value);
}
Serial.print(message);
Serial.println();
delay(2000);
}
void light(int ledPin, boolean on) {
if(on) {
digitalWrite(ledPin, HIGH);
Serial.println("LED HIGH");
} else {
digitalWrite(ledPin, LOW);
Serial.println("led low");
}
}
void reset() {
Serial.println("Void reset");
light(lightRight, false);
light(lightRoom, false);
light(lightGarage, false);
}
void on() {
Serial.println("Void on");
light(lightRight, true);
light(lightRoom, true);
light(lightGarage, true);
}
void off() {
Serial.println("Void off");
light(lightRight, false);
light(lightRoom, false);
light(lightGarage, false);
}
void blink(int delayn, int count) {
for (int j=0; j <= count; j++){
on();
delay(delayn);
off();
delay(delayn);
}
}
That example expects the PubNub publish to be "lightRight:1" and not {"lightRight": "1"}.

if equals not matched but should be. Aduino IDE Serial

I'm trying to get a raspberry pi communicating with an arduino over serial, hardware all is setup and there are some comms ok.
Problem I have is the arguments passed are not matched with a if, else if expression but really should be going off what is returned.
SerialCommand library handles what commands trigger what functions. We're only interested in sayHello() for this example. RaspberryPi sends "HELLO HELLO", which should receive "YES HELLO" but I always get "NO HELLO" suggesting no match on the arg HELLO. Obviously what argument was received is echoed back regardless and I receive back what should have matched, HELLO so I'm stumped at this stage, any suggestions?
EDIT: THanks to Thanushan Balakrishnan comment in his answer below, replacing
if(arg == "HELLO")
with the below fixes the problem.
if(strcmp("HELLO", arg) == 0)
_
// Demo Code for SerialCommand Library
// Steven Cogswell
// May 2011
// temp & Humidity
#include <DHT22.h>
#define DHT22_PIN 12
// Setup a DHT22 instance
DHT22 myDHT22(DHT22_PIN);
// tank water level
int sensorValue = 0;
int constrainedValue = 0;
int tankLevel = 0;
#define TANK_SENSOR 0
#define TANK_EMPTY 0
#define TANK_FULL 1023
/*RelayBrd */
//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 4;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 3;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 2;
boolean thisState = LOW;
#include <SerialCommand.h>
#define arduinoLED 13 // Arduino LED on board
SerialCommand sCmd; // The demo SerialCommand object
void setup() {
pinMode(arduinoLED, OUTPUT); // Configure the onboard LED for output
digitalWrite(arduinoLED, LOW); // default to LED off
//mySerial.begin(9600);
//mySerial.println("Ready");
Serial.begin(115200);
// Setup callbacks for SerialCommand commands
sCmd.addCommand("ON", LED_on); // Turns LED on
sCmd.addCommand("OFF", LED_off); // Turns LED off
sCmd.addCommand("getenv", getEnv);
sCmd.addCommand("relaybrd", relayBrd);
sCmd.addCommand("gettanklevel", getTankLevel);
sCmd.addCommand("setlouver", setLouver);
sCmd.addCommand("HELLO", sayHello); // Echos the string argument back
sCmd.addCommand("P", processCommand); // Converts two arguments to integers and echos them back
sCmd.setDefaultHandler(unrecognized); // Handler for command that isn't matched (says "What?")
Serial.println("Ready");
}
void loop() {
sCmd.readSerial(); // We don't do much, just process serial commands
// if (mySerial.available())
// Serial.write(mySerial.read());
// if (Serial.available())
// mySerial.write(Serial.read());
}
void relayBrd(){
char *arg;
char *arg1;
arg = sCmd.next();
arg1 = sCmd.next();
if (arg != NULL) {
//int num = atol(arg);
if (arg1 != NULL) {
int state = atol(arg1);
if (arg == "fan") {
//do something when var equals 1
registerWrite(1, state);
}else if(arg == "water"){
//do something when var equals 2
registerWrite(2, state);
}else if(arg == "mister"){
//do something when var equals 2
registerWrite(3, state);
}else if(arg == "heater"){
//do something when var equals 2
registerWrite(4, state);
}else{
// if nothing else matches, do the default
Serial.print("ERR got:");Serial.print(arg);Serial.println(":");
}
}else{
Serial.println("ERR Relay state missing 1/0");
}
}else{
Serial.println("ERR Relay argument missing fan/water/heater/mister");
}
}
// This method sends bits to the shift register:
void registerWrite(int whichPin, int whichState) {
Serial.print("Relay ");Serial.print(whichPin);Serial.print(" set to ");Serial.println(whichState);
// the bits you want to send
byte bitsToSend = 0;
// turn off the output so the pins don't light up
// while you're shifting bits:
digitalWrite(latchPin, LOW);
// turn on the next highest bit in bitsToSend:
bitWrite(bitsToSend, whichPin, whichState);
// shift the bits out:
shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
// turn on the output so the LEDs can light up:
digitalWrite(latchPin, HIGH);
}
void getEnv(){
Serial.println("26 degC");
DHT22_ERROR_t errorCode;
Serial.print("Requesting data...");
errorCode = myDHT22.readData();
switch(errorCode)
{
case DHT_ERROR_NONE:
Serial.print("Got Data ");
Serial.print(myDHT22.getTemperatureC());
Serial.print("C ");
Serial.print(myDHT22.getHumidity());
Serial.println("%");
break;
default:
Serial.print("ERR DHT22 ");
Serial.println(errorCode);
}
}
void getTankLevel(){
Serial.println("78% water level");
sensorValue = analogRead( TANK_SENSOR );
constrainedValue = constrain( sensorValue, TANK_EMPTY, TANK_FULL );
tankLevel = map( constrainedValue, TANK_EMPTY, TANK_FULL, 0, 100 );
}
void setLouver() {
char *arg;
arg = sCmd.next();
if (arg != NULL) {
if(arg == "OPEN"){
Serial.println("Louver OPEN");
}else if(arg == "CLOSE"){
Serial.println("Louver CLOSED");
}else{
Serial.print(arg);Serial.println(" not known");
}
}else{
Serial.println("Louver command missing OPEN/CLOSE");
}
}
void LED_on() {
Serial.println("LED on");
digitalWrite(arduinoLED, HIGH);
}
void LED_off() {
Serial.println("LED off");
digitalWrite(arduinoLED, LOW);
}
void sayHello() {
char *arg;
arg = sCmd.next(); // Get the next argument from the SerialCommand object buffer
if (arg != NULL) { // As long as it existed, take it
if (arg == "HELLO") { // As long as it existed, take it
Serial.print("YES ");
Serial.println(arg);
}else{
Serial.print("NO ");
Serial.println(arg);
}
}
else {
Serial.println("Hello Pi");
}
}
void processCommand() {
int aNumber;
char *arg;
Serial.println("We're in processCommand");
arg = sCmd.next();
if (arg != NULL) {
aNumber = atoi(arg); // Converts a char string to an integer
Serial.print("First argument was: ");
Serial.println(aNumber);
}
else {
Serial.println("No arguments");
}
arg = sCmd.next();
if (arg != NULL) {
aNumber = atol(arg);
Serial.print("Second argument was: ");
Serial.println(aNumber);
}
else {
Serial.println("No second argument");
}
}
// This gets set as the default handler, and gets called when no other command matches.
void unrecognized(const char *command) {
Serial.println("What?");
}
In the following function arg is a pointer. So arg has the address of the memory which holds "HELLO". so you should check *arg == "HELLO"
void sayHello()
{
char *arg;
arg = sCmd.next(); // Get the next argument from the SerialCommand object buffer
if (arg != NULL) { // As long as it existed, take it
if (strcmp("HELLO\n", arg) == 0) { <-------------------------- HERE
Serial.print("YES ");
Serial.println(arg);
}else{
Serial.print("NO ");
Serial.println(arg);
}
}
else {
Serial.println("Hello Pi");
}
}

Arduino making decision according to a packet received from serial port

The program listen to messages from serial port in the form or where first character (A or D) means analog or digital, the 2nd character - pin, the 3rd character - 1/0 or from 0 to 255. The markers < and > show the beginning and the end of packet.
For example, if packet is received, the light is turned on by digitalWrite(13,1)
But nothing happens. When I send via serial monitor, for instance: light is supposed to blink but it does not. The same with analogue outputs.
bool started = false;
bool ended = false;
char inData[5];
byte index;
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (Serial.available() > 0)
{
char inChar = Serial.read();
if (inChar == '<')
{
index = 0;
started = true;
ended = false;
}
else if (inChar == '>')
{
ended = true;
break;
}
else
{
if (index <= 4)
{
inData[index] = inChar;
index++;
}
}
if (started && ended)
{
if (inData[0] == 'A')
{
pinMode(inData[2],OUTPUT);
analogWrite(inData[2],inData[4]);
}
else if (inData[0] == 'D')
{
if (inData[4] == 1)
{
pinMode(inData[2],OUTPUT);
digitalWrite(inData[2],HIGH);
}
else if (inData[4] == 0)
{
pinMode(inData[2],OUTPUT);
digitalWrite(inData[2],LOW);
}
}
started = false;
ended = false;
index = 0;
}
}
Serial.println("Sending");
}
The following code will allow you to execute a method with an example serial string:
<power,led>
Once it processes this string, it'll execute the following method:
sendCommand(cmd, val);
See below for an example of how to turn on an LED on PIN 11.
#include <avr/pgmspace.h>
int IRledPin = 11;
#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
char inData[80];
byte index;
void setup() {
pinMode(IRledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Read all serial data available, as fast as possible
while (Serial.available() > 0) {
char inChar = Serial.read();
if (inChar == SOP) {
index = 0;
inData[index] = '\0';
started = true;
ended = false;
} else if (inChar == EOP) {
ended = true;
break;
} else {
if (index < 79) {
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if (started && ended) {
// The end of packet marker arrived. Process the packet
char *cmd = strtok(inData, ",");
if (cmd) {
char *val = strtok(NULL, ",");
if (val) {
sendCommand(cmd, val);
}
}
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
void sendCommand(char *command, char *value) {
if (strcmp(command,"power") == 0) {
power(value);
}
}
void power(char* value) {
if (strcmp(value, "led") == 0) {
digitalWrite(IRledPin, HIGH);
}
}
If the 2nd character is the pin, then you want inData[1] for your pin numbers instead of inData[2].
Why do you go from inData[0] to inData[2]? Wouldn't the second character be in inData[1]?
You're setting the pinMode to the actual value of inData[2]. That means to turn on pin 13, you need to send a carriage return character ('\r').
The code doesn't run below but it should help you to sort out your problem.
What it tries to do is split the inData into the Tokens[] array.
It then turns the ASCII data into integers with the atoi() statement.
Hope it helps.
Got the splitter from Segmentation Fault when using strtok_r
bool started = false;
bool ended = false;
char inData[5];
byte index;
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (Serial.available() > 0)
{
char inChar = Serial.read();
if (inChar == '<')
{
index = 0;
started = true;
ended = false;
}
else if (inChar == '>')
{
ended = true;
break;
}
else
{
inData[index] = inChar;
index++;
}
if (started && ended)
{
// https://stackoverflow.com/questions/2227198/segmentation-fault-when-using-strtok-r
// Splint up input data
char *p = inData;
char *tokens[50];
int i = 0;
while (i < 50) {
tokens[i] = strtok_r(p, ",", &p);
if (tokens[i] == NULL) {
break;
}
i++;
}
if (tokens[0] == '<A')
{
pinMode(tokens[1],OUTPUT);
analogWrite(tokens[2],tokens[3]);
}
else if (token[0] == '<D')
{
if (atoi(token[3]) == 1)
{
pinMode(atoi(token[1]),OUTPUT);
digitalWrite(atoi(token[1]),HIGH);
}
else if (atoi(tokens[3]) == 0)
{
pinMode(atoi(tokens[1]),OUTPUT);
digitalWrite(atoi(tokens[1]),LOW);
}
}
started = false;
ended = false;
index = 0;
}
}

Resources