Im working on a project I have to move the motor for x time when the button is pressed. during this time if the second button is pressed I need to stop the motor immadetly .
It would help me to understand your code if you can provide the English equivalent of your function names.
Also, if you use the AutoFormat tool in the Arduino IDE it will lay out your code consistently which makes it much easier to read.
I don't see any code to read a button so I presume you mean that you want to control the motor with characters received by Serial.
If you want a responsive program don't use delay(). The functions delay() and delayMicroseconds() block the Arduino until they complete. Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.
If you want the motor to stop after a certain time then save the value of millis() when the motor starts and keep checking for when the time expires. Something like
if (receivedChar == 'B') { // 'B' for begin - but use any character you like
motorStartMillis = millis();
// call function to make motor start
}
if (millis() - motorStartMillis >= motorRunTime) {
// call function to make motor stop
}
if (receivedChar == 'E') { // 'E' for end
// call function to make motor stop
}
I didnt added the buttons yet but I will add 3 button. left right and stop buttons.
when I pressed button 1 it should go to left for 10seconds. if I press the button 2 or the values from my sensor are too big Its need to be stopped.
I am trying to figure how can I do this for 2 days and couldt do it.
#include <Wire.h>
#include "i2c.h"
#include "i2c_MMA8451.h"
MMA8451 mma8451;
int RPWM = 5;
int LPWM = 6;
int L_EN = 7;
int R_EN = 8;
void setPWMfrequency(int freq) {
TCCR0B = TCCR0B & 0b11111000 | freq ;
}
void MotorActiveStatus(char Side, boolean s) {
boolean state = s;
if (Side == 'R') {
digitalWrite(R_EN, s);
}
if (Side == 'L') {
digitalWrite(L_EN, s);
}
}
void setMotor(char side, byte pwm) {
if (side == 'R') {
analogWrite(RPWM, pwm);
}
if (side == 'L') {
analogWrite(LPWM, pwm);
}
}
void closeMotor(char side) {
if (side == 'R') {
digitalWrite(RPWM, LOW);
}
if (side == 'L') {
digitalWrite(LPWM, LOW);
}
}
void goright() {
setMotor('R', 255);
}
void golleft() {
setMotor('L', 255);
}
void stopmotor() {
closeMotor('R');
closeMotor('L');
}
char receivedChar;
boolean newData = false;
void setup() {
setPWMfrequency(0x02);// timer 0 , 3.92KHz
for (int i = 5; i < 9; i++) {
pinMode(i, OUTPUT);
}
for (int i = 5; i < 9; i++) {
digitalWrite(i, LOW);
}
delay(1000);
MotorActiveStatus('R', true);
MotorActiveStatus('L', true);
Serial.begin(115200);
Serial.print("Probe MMA8451: ");
if (mma8451.initialize()) Serial.println("Sensor found!");
else
{
Serial.println("Sensor missing");
while (1) {};
}
}
float value = 0;
void loop() {
recvOneChar();//read the mma8451sensor
static float xyz_g[3];
mma8451.getMeasurement(xyz_g);
value = (xyz_g[0]);
Serial.println(value);
}
void recvOneChar() {
if (Serial.available() > 0) {
receivedChar = Serial.read();
newData = true;
}
}
gerdal:
I am trying to figure how can I do this for 2 days and couldt do it.
The concept I illustrated in Reply #2 can also be used with buttons - try it and see how you get on.
If you run into a problem please post the program that represents your best attempt and tell us in detail what it actually does and what you want it to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.
Robin2:
The concept I illustrated in Reply #2 can also be used with buttons - try it and see how you get on.
If you run into a problem please post the program that represents your best attempt and tell us in detail what it actually does and what you want it to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.
#include <Wire.h>
#include "i2c.h"
#include "i2c_MMA8451.h"
MMA8451 mma8451;
int RPWM = 5;
int LPWM = 6;
int L_EN = 7;
int R_EN = 8;
unsigned long motorStartMillis = 0;
void setPWMfrequency(int freq) {
TCCR0B = TCCR0B & 0b11111000 | freq ;
}
void MotorActiveStatus(char Side, boolean s) {
boolean state = s;
if (Side == 'R') {
digitalWrite(R_EN, s);
}
if (Side == 'L') {
digitalWrite(L_EN, s);
}
}
void setMotor(char side, byte pwm) {
if (side == 'R') {
analogWrite(RPWM, pwm);
}
if (side == 'L') {
analogWrite(LPWM, pwm);
}
}
void closeMotor(char side) {
if (side == 'R') {
digitalWrite(RPWM, LOW);
}
if (side == 'L') {
digitalWrite(LPWM, LOW);
}
}
void goright() {
setMotor('R', 255);
}
void gorights() {
setMotor('R', 155);
}
void goleft() {
setMotor('L', 255);
}
void golefts() {
setMotor('L', 155);
}
void stopmotor() {
closeMotor('R');
closeMotor('L');
}
char receivedChar;
boolean newData = false;
void setup() {
setPWMfrequency(0x02);// timer 0 , 3.92KHz
for (int i = 5; i < 9; i++) {
pinMode(i, OUTPUT);
}
for (int i = 5; i < 9; i++) {
digitalWrite(i, LOW);
}
delay(1000);
MotorActiveStatus('R', true);
MotorActiveStatus('L', true);
Serial.begin(115200);
// Serial.print("Probe MMA8451: ");
// if (mma8451.initialize()) Serial.println("Sensor found!");
// else
// {
// Serial.println("Sensor missing");
// while (1) {};
// }
}
float value = 0;
void loop() {
recvOneChar();
int motorRunTime = 100000;
if (receivedChar == 'a') { // 'B' for begin - but use any character you like
motorStartMillis = millis();
gorights();// call function to make motor start
Serial.println("working");
}
if (receivedChar == 's') { // 'B' for begin - but use any character you like
motorStartMillis = millis();
golefts();// call function to make motor start
Serial.println("working");
}
if (receivedChar == 'd') { // 'B' for begin - but use any character you like
motorStartMillis = millis();
goright();// call function to make motor start
Serial.println("working");
}
if (receivedChar == 'f') { // 'B' for begin - but use any character you like
motorStartMillis = millis();
goleft();// call function to make motor start
Serial.println("working");
}
if (millis() - motorStartMillis >= motorRunTime) {
stopmotor();// call function to make motor stop
}
if (receivedChar == 'e') { // 'E' for end
stopmotor(); // call function to make motor stop
}
// static float xyz_g[3];
// mma8451.getMeasurement(xyz_g);
//value = (xyz_g[0]);
//Serial.println(value);
}
void recvOneChar() {
if (Serial.available() > 0) {
receivedChar = Serial.read();
gerdal:
now I have 2 problem
first my times are not aqqured maybe because of the motor freq.
second when I acivate rf433 listening my motor starts spining.
The first rule of debugging is deal with one problem at a time. So leave out the wireless code until you have the "times" working.
I don't understand what you mean by "first my times are not aqqured maybe because of the motor freq."