Send stepper to position and back AccelStepper - arduino

At the moment I am working on a code that should send a plateau to a certain position and back. The plateau is being moved by the stepper. My goal is to press '4' on the keyboard and that then the steppers moves to a certain position. However, I am not succeding in this. I am using AccelStepper and at the moment I have the following code:
#include <AccelStepper.h>
AccelStepper stepper(1, 5, 4);
int spd = 1000; // The current speed in steps/second
int sign = 1; // Either 1, 0 or -1
int buttonState = 0;
void setup()
{
Serial.begin(9600);
stepper.setMaxSpeed(1000);
stepper.setSpeed(1000);
}
void loop()
{
char c;
if (Serial.available()) {
c = Serial.read();
if (c == 'f') { // forward
sign = 1;
}
if (c == 'r') { // reverse
sign = -1;
}
if (c == 's') { // stop
sign = 0;
}
if (c == '1') { // super slow
spd = 100;
}
if (c == '2') { // medium
spd = 900;
}
if (c == '3') { // fast
spd = 1000;
}
if (c == '4') {
//not working, does anyone know how to do this?
stepper.moveTo(500);
}
stepper.setSpeed(sign * spd);
}
stepper.runSpeed();
}
Does anyone have tips or know how to achieve this?
Thanks is advance!

Related

Arduino code for multiple buttons and LED matrix

I'm trying to build a system using Arduino Uno, 8x8 LED matrix and 3 push buttons. The goal of the system is to display 3 different characters upon pushing the 3 buttons correspondingly. For instance, I've chosen the letters A,B,C. When the button corresponding to A is pressed, the letter A must be displayed and similar for B and C too. I'm kinda stuck in this code, where it seems logically correct for me but I have no idea why it isn't working. Thanks in advance.
#include "LedControlMS.h"
#define NBR_MTX 1
LedControl lc=LedControl(4,3,2, NBR_MTX);//
const int buttonPinA = 8;
const int buttonPinB = 9;
const int buttonPinC = 10;
char ip2;
void setup()
{
Serial.begin(9600);
for (int i=0; i< NBR_MTX; i++)
{
lc.shutdown(i,false);
lc.setIntensity(i,8);
lc.clearDisplay(i);
delay(100);
}
}
void Fun1()
{
lc.writeString(0,"A");
delay(500);
lc.clearAll();
}
void Fun2{
lc.writeString(0,"B");
delay(500);
lc.clearAll();
}
void Fun3()
{
lc.writeString(0,"C");
delay(500);
lc.clearAll();
}
void loop(){
if( digitalRead(buttonPinA) == HIGH){
ip2 = 1;}
else if(digitalRead(buttonPinB) == HIGH){
ip2 = 2;}
else if(digitalRead(buttonPinC) == HIGH){
ip2 = 3;
}
if(ip2 == '1'){
for(int i=1;i<=6;i++){
Fun1();
}
}
else if(ip2 == '2')
{
for(int i=1;i<=6;i++){
Fun2();}
}
else if(ip2 == '3'){
for(int i=1;i<=6;i++){
Fun3();}
}}
You're setting your char ip2 variable as an integer and then checking it as a character. You will see in this ASCII Table that '1' is equal to 31 as in integer, '2' is equal to 32 and so on.
Replacing the first few lines of you main loop with the code below should fix your problem. If not, I would check the documentation of the led library you're using and make sure you're implementing it correctly.
if( digitalRead(buttonPinA) == HIGH)
{
ip2 = '1';
}
else if(digitalRead(buttonPinB) == HIGH)
{
ip2 = '2';
}
else if(digitalRead(buttonPinC) == HIGH)
{
ip2 = '3';
}

Arduino:Problems using A6 and A7 with push buttons

I'm developing an Arduino project with push buttons. I need some action to be executed after a button is pressed. The problem is that whenever I use buttons on A6 and A7 pins, I get unexpected results like a button receives HIGH without being physically pressed. As long as I use digital pins with buttons, the code works just fine.
Could you please explain to me what may be the problem using A6 and A7 pins or maybe there is some tricky thing with these pins that I need to consider.
Thanks a lot!
Here is my code:
int buttonBack = A6;
int flagLeft, flagRight;
int eventBack;
int currentButtonStatus = 0;
unsigned long currentButtonStatusStart1;
unsigned long currentButtonStatusStart2;
unsigned long currentButtonStatusStart3;
const int delayFalse = 30;
const int delayLongSingleClick = 300;
const int delayDeltaDoubleClick = 200;
void setup() {
pinMode(buttonBack,INPUT);
}
void loop() {
int eventBack = changeButtonStatus();
if (eventBack > 0) {
if(eventBack == 1) { // single button press
flagLeft = 1; flagRight = 0;
// move motor to the left code goes here
}
if(eventBack == 4) {
// long button press
flagLeft = 0; flagRight = 1;
// move motor to the right code goes here
}
}
}
int changeButtonStatus() {
int event = 0;
int currentButtonClick = analogRead(buttonBack);
unsigned long timeButton = millis();
switch(currentButtonStatus) {
case 0:
if(currentButtonClick) {
currentButtonStatus = 1;
currentButtonStatusStart1 = millis();
} else {
}
break;
case 1:
if(currentButtonClick) {
if(timeButton - currentButtonStatusStart1 >= delayLongSingleClick) {
event = 3;
}
} else {
if(timeButton - currentButtonStatusStart1 < delayFalse) {
currentButtonStatus = 0;
event = 0;
} else if(timeButton - currentButtonStatusStart1 < delayLongSingleClick) {
currentButtonStatus = 2;
currentButtonStatusStart2 = millis();
} else {
currentButtonStatus = 0;
event = 4;
}
}
break;
case 2:
if(currentButtonClick) {
if(timeButton - currentButtonStatusStart2 < delayFalse) {
currentButtonStatus = 1;
} else {
currentButtonStatus = 3;
currentButtonStatusStart3 = millis();
}
} else {
if(timeButton - currentButtonStatusStart2 > delayDeltaDoubleClick) {
currentButtonStatus = 0;
event = 1;
}
}
break;
case 3:
if(currentButtonClick) {
} else {
if(timeButton - currentButtonStatusStart3 < delayFalse) {
} else {
event = 2;
currentButtonStatus = 0;
}
}
break;
}
return event;
}
A6 and A7 do not have GPIO capability. They can only be used as analog inputs which means that you will need to provide external pullups if you are attempting to emulate digital inputs with them.

Why I could not read the other received sms's except the first SMS?

I have to read the incoming SMS on my gsm module SIM900 (which is connected to Arduino), and I want to print the sender number and message on to serial monitor.
I first configure gsm module with AT commands and Response() function will give me the response to AT commands.
as any SMS will be in the following pattern
+CMT: "[Mobile number]", "[Date and Time]"
[message body]
So, I first extract +CMT and after that I will take mobile number and atlast we have message body. The code I have used is
char RcvdMsg[200] = "";
int RcvdCheck = 0;
int RcvdConf = 0;
int index = 0;
int RcvdEnd = 0;
char MsgMob[15];
char MsgTxt[50];
int MsgLength = 0;
void Config() // This function is configuring our SIM900 module i.e. sending the initial AT commands
{
delay(1000);
Serial.print("ATE0\r");
Response();
Serial.print("AT\r");
Response();
Serial.print("AT+CMGF=1\r");
Response();
Serial.print("AT+CNMI=1,2,0,0,0\r");
Response();
}
void setup()
{
Serial.begin(9600);
Config();
}
void loop()
{
RecSMS();
}
void Response() // Get the Response of each AT Command
{
int count = 0;
Serial.println();
while(1)
{
if(Serial.available())
{
char data =Serial.read();
if(data == 'K'){Serial.println("OK");break;}
if(data == 'R'){Serial.println("GSM Not Working");break;}
}
count++;
delay(10);
if(count == 1000){Serial.println("GSM not Found");break;}
}
}
void RecSMS() // Receiving the SMS and extracting the Sender Mobile number & Message Text
{
if(Serial.available())
{
char data = Serial.read();
if(data == '+'){RcvdCheck = 1;}
if((data == 'C') && (RcvdCheck == 1)){RcvdCheck = 2;}
if((data == 'M') && (RcvdCheck == 2)){RcvdCheck = 3;}
if((data == 'T') && (RcvdCheck == 3)){RcvdCheck = 4;}
if(RcvdCheck == 4){RcvdConf = 1; RcvdCheck = 0;}
if(RcvdConf == 1)
{
if(data == '\n'){RcvdEnd++;}
if(RcvdEnd == 3){RcvdEnd = 0;}
RcvdMsg[index] = data;
index++;
if(RcvdEnd == 2){RcvdConf = 0;MsgLength = index-2;index = 0;}
if(RcvdConf == 0)
{
Serial.print("Mobile Number is: ");
for(int x = 4;x < 17;x++)
{
MsgMob[x-4] = RcvdMsg[x];
Serial.print(MsgMob[x-4]);
}
Serial.println();
Serial.print("Message Text: ");
for(int x = 46; x < MsgLength; x++)
{
MsgTxt[x-46] = RcvdMsg[x];
Serial.print(MsgTxt[x-46]);
}
Serial.println();
Serial.flush();
}
}
}
}
The problem of the code is
After receiving first SMS I am getting my mobile number and message body. After that I am only getting sender number printed on to my serial monitor but not the message body.
Where It has gone wrong. I could not understood.
Please help me.......Thanks in advance.
If it does work the first time, but not subsequent times it probably has to do with some variables not being reset. You declare all of your variables at the top of the file even though they are only needed in the RecSMS() function. Try moving the declarations to the top of RecSMS().
void RecSMS() {
char RcvdMsg[200] = "";
int RcvdCheck = 0;
int RcvdConf = 0;
int index = 0;
int RcvdEnd = 0;
char MsgMob[15];
char MsgTxt[50];
int MsgLength = 0;
if(Serial.available()) {
// Rest of the code goes here
Thankyou #Michael. I think this also solves the issue.
The problem I found in the code is, we are not resetting all the variables in RecSMS function. So to solve this keep this below code before the Serial.flush() statement.
RcvdCheck = 0;
RcvdConf = 0;
index = 0;
RcvdEnd = 0;
MsgMob[15];
MsgTxt[50];
MsgLength = 0;
This will solve the problem

Problems using a class with arduino

I am trying to make a micro-mouse maze solving robot, and I'm running into some issues with my class called Mouse. I get the error: expected ';' before 'right_motor' and 'left_motor'. I have no idea what is wrong with the code. I also am not sure exactly where I should declare my objects left and right motors. Here is all my code, thank you for your help.
Mouse.h:
#ifndef _MOUSE_H_
#define _MOUSE_H_
class Mouse
{
private:
int speed;
int motor_num;
int direction;
public:
Mouse(int motor_number);
~Mouse();
void run(int speed, int direction);
};
#endif
Mouse.cpp:
#include "mouse.h"
Mouse::Mouse(int motor_number)
{
motor_num = motor_number;
speed = 0;
direction = 0;
return;
}
// Digital pin 11: DC Motor #1 / Stepper #1 (activation/speed control)
// Digital pin 3: DC Motor #2 / Stepper #1 (activation/speed control)
// Digital pin 5: DC Motor #3 / Stepper #2 (activation/speed control)
// Digital pin 6: DC Motor #4 / Stepper #2 (activation/speed control)
void Mouse::run(int speed, int direction)
{
int M1 = 11;
int M2 = 3;
int M3 = 5;
int M4 = 6;
if(motor_num == 1)
{
if(direction == 1)
{
analogWrite(M1, speed);
}
if(direction == -1)
{
// FIXME: how do i do backwards?
}
if(direction == 0)
{
digitalWrite(M1, LOW);
}
}
if(motor_num == 2)
{
if(direction == 1)
{
analogWrite(M2, speed);
}
if(direction == -1)
{
// FIXME: how do i do backwards?
}
if(direction == 0)
{
digitalWrite(M2, LOW);
}
}
if(motor_num == 3)
{
if(direction == 1)
{
analogWrite(M3, speed);
}
if(direction == -1)
{
// FIXME: how do i do backwards?
}
if(direction == 0)
{
digitalWrite(M3, LOW);
}
}
if(motor_num == 4)
{
if(direction == 1)
{
analogWrite(M4, speed);
}
if(direction == -1)
{
// FIXME: how do i do backwards?
}
if(direction == 0)
{
digitalWrite(M4, LOW);
}
}
return;
}
Code in the Arduino sketch:
#include "mouse.h"
void setup()
{
//begin communication with serial port:
Serial.begin(9600);
//pinmodes setup:
//declare variables:
Mouse right_motor(1);
Mouse left_motor(2);
}
void loop()
{
//assign values to their respective variables:
//BEGIN PROGRAM
}
Change your class in MyMouse (...and update all reference of it), I think Mouse is an internal Arduino library
then, pay attention on Uppercase chars sometimes Mouse is uppercase and sometimes is lowercase
Edit:
this is Mouse Arduino library
http://www.arduino.cc/en/Reference/MouseKeyboard

Arduino memory game

This game has 4 leds and 4 buttons. the game is turning RANDOMLY those leds ON and OFF.
The player should be able to push the right button whenever he sees one led to be turned ON.
The leds should be turning ON and OFF with incerasing speed, so reaction time of a player is shorter and shorter
I have this code but i just know how to add more leds and buttons.
const int BUTTON1 = A0;
const int BUTTON2 = A1;
const int BUTTON3 = A2;
const int BUTTON4 = A3;
int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int LED4 = 5;
int ran;
int right = 0;
int ledOrder[9];
int guessOrder[9];
void setup()
{
Serial.begin(9600);
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(LED3,OUTPUT);
pinMode(LED4,OUTPUT);
pinMode(BUTTON1,INPUT);
pinMode(BUTTON2,INPUT);
pinMode(BUTTON3,INPUT);
pinMode(BUTTON4,INPUT);
}
void randomLed() {
for (int i = 0; i < 9; i++) {
ran = random(1,20);
if (ran < 11) {
digitalWrite(LED1,HIGH);
delay(500);
digitalWrite(LED1,LOW);
ledOrder[i] = 1;
}
else {
digitalWrite(LED2,HIGH);
delay(500);
digitalWrite(LED2,LOW);
ledOrder[i] = 2;
}
delay(500);
}
}
void btnClick() {
int ans = 0;
while (ans < 9) {
if (digitalRead(BUTTON1) == HIGH) {
guessOrder[ans] = 1;
ans++;
while (digitalRead(BUTTON1) == HIGH) {
}
}
else if (digitalRead(BUTTON2) == HIGH) {
guessOrder[ans] = 2;
ans++;
while (digitalRead(BUTTON2) == HIGH) {
}
}
}
}
void loop() {
Serial.print("Press button1 to start \n");
while (digitalRead(BUTTON1) == LOW) {
}
randomLed();
btnClick();
for (int i = 0; i < 9; i = i + 1) {
Serial.print("Guess: ");
Serial.print(guessOrder[i]);
Serial.print(" Answer: ");
Serial.print(ledOrder[i]);
if (guessOrder[i] == ledOrder[i]) {
Serial.print(" Right");
right++;
} else {
Serial.print(" Wrong");
}
Serial.print("\n ");
}
Serial.print(right);
Serial.print("/9\n");
delay(2000);
}
If I understand your question correctly, you would like to increase the led blink rate(when the LED's come on) each time the user gets the previous game right. If this is true you should try to put loop in for/if the user got the the previous one(game) right it will increase blink rate by a simple increment( I++;). Comment if you feel this is clarified enough or if your asking for something else

Resources