Problems using a class with arduino - 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

Related

Serial communication stuck after first attempt

I am using arduino Mega 2560 to control some vibration motors with touchdesigner through Serial Communication. I maped the pixels to control each motors, It works for a few seconds and gets stuck very soon. Is there anything wrong with my code?
Here is my arduino sketch:
#define MOTOR_COUNT 12
int motors[MOTOR_COUNT];
void setup()
{
Serial.begin(115200);
Serial.println("Ready to receive frames.");
for (int i = 0; i <= 12; i++) {
pinMode(i, OUTPUT);
}
void loop()
{
if (Serial.available())
{
char c = Serial.peek();
if (!(c >= '0' && c <= '9'))
{
Serial.read(); // Discard non-digit character
}
else if (Serial.read() == '\n')
{
for (uint16_t i = 0; i < MOTOR_COUNT; i++)
{
motors[i] = Serial.parseInt();
Serial.print("motor ");
Serial.print(i);
Serial.print(":");
Serial.println(motors[i]);
if (i <= 12) {
analogWrite(i + 2, motors[i]);
}
}
}
}
}

How to make two servos work one after another? And stop shaking?

For my project, I am trying to make a page-turner that's controlled by two servos --- one to flip (with a horn) and one to lift (with a wheel). My goal here is when I press the RIGHT button, my wheel should roll a page clockwise FIRST then my horn will swipe from left to right (the same but opposite for my LEFT button) to turn a page.
While I am trying to adjust the angles, I have my button right servo works the way I wanted to but it's shaking, on the other hand, I have my button right servo works smooth and continuously but the flip and the turn were both active at the same time.
#include <Servo.h>
const int swipservo = 8; //yellow
const int liftservo = 9; //green
Servo servo1; // create servo object to control a servo
Servo servo2;
const int leftbutton = 11; //yellow = turn left
const int rightbutton = 12; //blue = turn right
int pos1 = 0; // variable to store the servo position
int pos2 = 0;
void setup() {
servo1.attach (swipservo);
servo2.attach (liftservo);
pinMode(leftbutton , INPUT);
pinMode(rightbutton , INPUT);
}
void loop() {
test();
}
void test() {
if (digitalRead(rightbutton) == HIGH) {
for (pos2 = 0; pos2 < 350; pos2++) {
if (pos2 < 180) {
servo2.write (pos2);
delay(15);
}
if (pos1 > 180) {
servo1.write(0);
delay(15);
servo2.write(0);
delay(15);
}
else if (pos2 >= 180) {
servo1.write(pos1);
delay(15);
pos1 ++;
}
//go back to 0 for POS 2
}
}
if (digitalRead(leftbutton) == HIGH) {
for (pos2 = 160; pos2 > 0; pos2++) {
if (pos2 < 0) {
servo2.write (pos2);
delay(5);
}
if (pos1 >= 180) {
servo1.write(0);
delay(15);
servo2.write(0);
delay(15);
}
else if (pos2 > 0) {
servo2.write(pos2);
delay(15);
}
if (pos2 <= 0) {
servo1.write(pos1);
delay(15);
pos1 ++;
}
}
}
}

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';
}

Servo motors not working when called upon using I2C

So I'm using this movement() function inside the slave Arduino Uno, which basically handles two stepper motors and one servo motors, when I call this function through the serial read of the Arduino Uno it works perfectly, both the steppers and the servo.
But I have a master Arduino Mega interfaced with the Uno (mentioned above) when I send a wire message from the mega to Arduino to run the movement function in the Uno, the steppers work fine, but the servo does not work, although again I mention that when the function is called upon using the serial read of the Uno it works absolutely fine.
I'm using an Adafruit motor shield on the Arduino Uno as well.
Ok so this is the Uno (slave code)
void setup()
{
servo1.attach(9);
Serial.begin(9600);
Wire.begin(5);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void loop()
{
//other stuff
}
void receiveEvent(int howMany)
{
while(Wire.available())
{
char c = Wire.read();
int From = Wire.read();
int To = Wire.read();
int x = 0;
movement(From,To);
}
}
int movement(int from,int to)
{
int X_From = from / 10;
int X_To = to/10;
int Y_From = from % 10;
int Y_To = to % 10;
/* First we have to get the motor position and move the motor to the from position */
/* === START === */
int diff_X = X_From - motor_X;
int diff_Y = Y_From - motor_Y;
int Y_Value = abs(diff_Y * Y_Steps);
servoDown();
if(diff_X != 0)
{
if(diff_X > 0)
{
/* Positive value means the X_Axis motor will move in the forward direction */
int X_Value = abs(diff_X * X_Steps);
X_Forward(X_Value);
}
else
if(diff_X < 0)
{
/* Negative value means the X_Axis motor will move in the backward direction */
int X_Value = abs(diff_X * 400);
X_Backwards(X_Value);
}
}
//Serial.println(X_Value); Serial.println(Y_Value);
if(diff_Y != 0)
{
if(diff_Y > 0)
{
Y_Forward(Y_Value);
}
else
if(diff_Y < 0)
{
Y_Backwards(Y_Value);
}
}
Serial.println("Motor movement done");
/* === END === */
motor_X = X_From;
motor_Y = Y_From;
int diff_X_2 = X_To - motor_X;
int diff_Y_2 = Y_To - motor_Y;
int Y_Value2 = abs(diff_Y_2 * Y_Steps);
servoUp();
if(diff_X_2 != 0)
{
if(diff_X_2 > 0)
{
/* Positive value means the X_Axis motor will move in the forward direction */
int X_Value2 = abs(diff_X_2 * X_Steps);
X_Forward(X_Value2);
}
else
if(diff_X_2 < 0)
{
/* Negative value means the X_Axis motor will move in the backward direction */
int X_Value2 = abs(diff_X_2 * 400);
X_Backwards(X_Value2);
}
}
if(diff_Y_2 != 0)
{
if(diff_Y_2 > 0)
{
Y_Forward(Y_Value2);
}
else
if(diff_Y_2 < 0)
{
Y_Backwards(Y_Value2);
}
}
//Serial.println(X_Value2); Serial.println(Y_Value2);
motor_X = X_To;
motor_Y = Y_To;
Serial.println("piece movement done");
return 1;
}
void servoUp()
{
for (i = 100; i >= 0; i--)
{
servo1.write(i);
// delay(20);
}
}
void servoDown()
{
for (i = 0; i < 100; i++)
{
servo1.write(i);
//delay(20);
}
}
This is the master Mega code
void setup()
{
Wire.begin();
}
void loop()
{
//check if a condition appeared from sensor and then send the data to uno to run the movement function through sendData()
sendData(From,To);
}
void sendData(int From,int To)
{
Wire.beginTransmission(5);
Wire.write('f');
Wire.write(From);
Wire.write(To);
Wire.endTransmission();
}
Any suggestions?
Looks like the servo movement does not depend no the data transmitted. Did you check if the intended values are actually received? One thing to keep in mind: read() reads a single byte. Are you trying to transmit a value larger than 255?

Send stepper to position and back AccelStepper

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!

Resources