So, i want to tramsmit data between an arduino leonardo and an atmega328p
I uploaded this code to the atmega:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
float val = analogRead(1);
float vol = (val / 34.1);
Serial.println(vol);
delay(500);
}
and this code to the arduino:
#include "SevSeg.h"
SevSeg sevseg; //Initiate a seven segment controller object
float vol = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
byte numDigits = 4;
byte digitPins[] = {2, 3, 4, 5};
byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);
sevseg.setBrightness(90);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0)
{
float vol = Serial.read();
}
sevseg.setNumber(vol, 2);
sevseg.refreshDisplay(); // Must run repeatedly
}
My plan is for the atmega to measure voltage on the analog pin 1. I want to make a voltmeter, so i made a voltage divider on the input that makes the 30 V go down to 5V, and the code interpreters it back to 30 V. Then it sends the data through serial pins to arduino, and it displays it on a seven segment display.
The arduino doesn' recieve any information, so any idea to what's wrong?
JEREDEKK:
So, i want to tramsmit data between an arduino leonardo and an atmega328p
If you want to use pins 0 and 1 on an Arduino Leonardo for serial communication, you need to use Serial1 and not Serial.
Serial (without 1) on Leonardo will "talk" to USB only not to pins 0/1 (take a look at here).
So use:
Serial1.begin(9600);
if (Serial1.available() > 0)...
Serial1.read();
and so on
As already mentioned, a wiring diagram would be needed.
To get it working connect :
TX --- RX
RX --- TX
GND --- GND
BTW: using the word "atmega" may be somehow ambigous
because the microcontroller working on Arduino Leonardo is an ATmega32u4
so both microcontrollers in your setup are ATmegas
There are two serial ports on the Leonardo, Serial and Serial1. The Serial instance is via the onboard USB-to-serial conversion process. Serial1 is via the RX and TX pins.
If you don't have anything connected to the RX and TX pins, using Serial1 is not going to accomplish anything.
Here's the tested Serial code for Arduino Leonardo: (You can change the baud rate as per your requirement)
void setup()
{
Serial.begin(115200); //This pipes to the serial monitor
while(!Serial);
Serial1.begin(115200); //This is the UART, pipes to sensors attached to board
}
void loop()
{
if( Serial.available() )
{
int inByte = Serial.read();
Serial1.write( inByte );
}
if( Serial1.available() )
{
int inByte = Serial1.read();
Serial.write( inByte );
}
}