Hello, im am currently struggling connecting my arduino mega to my arduino uno. So i connected my atmega2560 on TX1 to RX0 on uno, and the common ground as well. So the atmega is receiving some stepper motor speed values via the nrf24l01 module. After receiving the values, it uses serial.print function to send it to the uno where it will set the speed of the motor accordingly. However it is not setting the speed, its only locking the stepper motor for a fraction of millisecond and runs it again at the same speed it was previously running. Note that the UNO is not connected to any power supply. Now, when i connect the usb cable to the UNO, then the speed is set, otherwise if i remove the usb, it only locks it for a fraction of millisecond, then continues to run the motor at the same speed it was previously running instead of changing it accordingly. Do u think i need to power the uno as well? or the issue is elsewhere?
Why are you using two Arduinos and not just the Mega ? The Mega can do everything that the Uno can and more
Its a long code, so when i run the stepper motor on the mega, it blocks then run again due to the code being executed
Adding a Uno to the project is not the answer, fixing the code on the Mega is
Please post the full sketch that does not work on the Mega
Mega code:#include <AccelStepper.h>
#include <Adafruit_MLX90614.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <SPI.h>
#include <RF24.h>
#include <RTClib.h>
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified();
#define CE_PIN 7
#define CSN_PIN 8
RF24 radio(CE_PIN, CSN_PIN);
// Address through which two modules communicate.
const byte address1[6] = "00001";
const byte address2[6] = "00002";
unsigned long previousMillis = 0;
const long interval = 1000;
const int analogInPin = A0;
int sensorValue = 0;
float voltage = 0;
float current = 0;
char t[32]; // Increased buffer size for the timestamp
enum TaskState {
READ_TEMP,
READ_CURRENT,
READ_ACCEL,
PRINT_ALL,
TRANSMIT_DATA,
IDLE
};
TaskState taskState = IDLE;
float ambientTemp = 0.0;
float objectTemp = 0.0;
float acceleration_magnitude = 0.0;
float newSpeed = 0.0;
RTC_DS3231 rtc;
void setup() {
Serial.begin(9600); // Serial communication with the computer
Serial1.begin(9600); // Serial communication with the Uno
rtc.begin();
mlx.begin();
rtc.adjust(DateTime(2024, 7, 2, 18, 27, 0));
if (!mlx.begin()) {
Serial.println("Error connecting to MLX sensor. Check wiring.");
}
if (!accel.begin()) {
Serial.println("No ADXL345 detected. Check wiring.");
}
radio.begin();
radio.openWritingPipe(address1);
radio.openReadingPipe(1, address2);
radio.setPALevel(RF24_PA_MIN); // Ensure maximum power level for better communication
Serial.println("Setup complete");
}
void loop() {
unsigned long currentMillis = millis();
switch (taskState) {
case READ_TEMP:
ambientTemp = mlx.readAmbientTempC();
if (isnan(ambientTemp)) {
ambientTemp = 0;
}
objectTemp = mlx.readObjectTempC();
if (isnan(objectTemp)) {
objectTemp = 0;
}
taskState = READ_CURRENT;
break;
case READ_CURRENT:
sensorValue = analogRead(A2);
voltage = sensorValue * 5.0 / 1023.0;
current = (voltage - 2.5) / 0.185;
taskState = READ_ACCEL;
break;
case READ_ACCEL:
sensors_event_t event;
accel.getEvent(&event);
acceleration_magnitude = sqrt(event.acceleration.x * event.acceleration.x +
event.acceleration.y * event.acceleration.y +
event.acceleration.z * event.acceleration.z);
if (isnan(acceleration_magnitude)) {
acceleration_magnitude = 0;
}
taskState = PRINT_ALL;
break;
case PRINT_ALL:
taskState = TRANSMIT_DATA;
break;
case TRANSMIT_DATA:
int analogValue = analogRead(A0);
float voltage = analogValue * (5.0 / 1023.0);
float dB = 20 * log10(voltage);
radio.stopListening();
delay(500); // Allow time for switching modes
DateTime now = rtc.now();
sprintf(t, "%02d-%02d-%02d %02d:%02d:%02d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
String data = String(objectTemp) + "," + String(current) + "," + String(acceleration_magnitude) + "," + String(dB) ;
char text[64]; // Use buffer size 64
data.toCharArray(text, 64);
radio.write(&text, sizeof(text));
Serial.println("Transmitting via RF: " + data);
// Transmit the same data via serial connection to Uno
// Serial1.println(data);
taskState = IDLE;
break;
case IDLE:
break;
}
if (taskState == IDLE && (currentMillis - previousMillis >= interval)) {
previousMillis = currentMillis;
taskState = READ_TEMP;
}
radio.startListening();
delay(500); // Allow time for switching modes
if (radio.available()) {
char text1[64] = {0}; // Use buffer size 64
radio.read(&text1, sizeof(text1));
Serial.println(String(text1));
Serial1.println(String(text1));
}
}
Uno code:#include <AccelStepper.h>
#include <Adafruit_MLX90614.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <SPI.h>
#include <RF24.h>
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified();
unsigned long previousMillis = 0; // Stores the last time temperature and current were read
const long interval = 1000; // Interval at which to read temperature and current (milliseconds)
const int analogInPin = A0; // Analog input pin that the current sensor is attached to
int sensorValue = 0; // Value read from the sensor
float voltage = 0; // Variable to store the voltage
float current = 0; // Variable to store the current
enum TaskState {
READ_TEMP,
READ_CURRENT,
READ_ACCEL,
PRINT_ALL,
TRANSMIT_ACCEL,
IDLE
};
TaskState taskState = IDLE;
float ambientTemp = 0.0;
float objectTemp = 0.0;
float acceleration_magnitude = 0.0;
void setup() {
Serial.begin(9600);
// Serial1.begin(9600); // Serial communication with Arduino Uno
stepper.setMaxSpeed(20000); // Maximum speed in steps per second
stepper.setSpeed(5000); // Initial speed in steps per second
}
void loop() {
// Read input from Serial monitor
if (Serial.available() > 0) {
String input = Serial.readString();
float newSpeed = input.toFloat();
// Only set the speed to the new value if it is not already set
if (newSpeed != stepper.speed() && newSpeed != 0) {
stepper.setSpeed(newSpeed);
Serial.print("Speed set to: ");
Serial.println(newSpeed);
}
}
// Continuous motor run
stepper.runSpeed();
}
Please edit your post and add code tags
In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
Um, yeah. One Arduino to another should be TX (a) to Rx (b), Tx (b) to Rx (a), gnd to gnd and that alone doesn't supply power to the Arduino Uno.
Also, post both codes to see how you're reading
on the Uno. Something tells me you're not sending and receiving exactly what you think you are.
Well, you've commented this out, so...
this one was not meant to be sent thats why i commented it, you can see below in the if radio.available block, i sent the speed value
Ok, but your code isn't formatted correctly in code blocks, so I'm not looking too closely.
After correcting the physical connection and properly powering both Arduinos and other devices, does the problem remain?