So I have a code that works just fine with my uno to use 8 led lights controlled by bluetooth using sliders on mic app inventor. Seeing that the uno only has 6 pwm outputs, I upgraded to the mega2560, so I can use dimmers on all of my 8 led lights. I wired and configured everything exactly the same. The code and bluetooth works just fine on my uno, but nothing from the MEGA. Do I need to change my code for the processor? Here is my code which works just fine for the uno
#include<SoftwareSerial.h>
SoftwareSerial future(0,1);
int led1=4;
int led2=5;
int led3=6;
int led4=7;
int led5=8;
int led6=9;
int led7=10;
int led8=11;
void setup() {
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(led5,OUTPUT);
pinMode(led6,OUTPUT);
pinMode(led7,OUTPUT);
pinMode(led8,OUTPUT);
future.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (future.available()){
int value1=future.parseInt();
int value2=future.parseInt();
int value3=future.parseInt();
int value4=future.parseInt();
int value5=future.parseInt();
int value6=future.parseInt();
int value7=future.parseInt();
int value8=future.parseInt();
if(future.read() =='\n') {
analogWrite(led1,value1);
analogWrite(led2,value2);
analogWrite(led3,value3);
analogWrite(led4,value4);
analogWrite(led5,value5);
analogWrite(led6,value6);
analogWrite(led7,value7);
analogWrite(led8,value8);
}}}
Remind me - which are the PWM pins on a Uno?
Why is your code so long?
Hey there. So I can't totally explain the code. I found it on youtube, and it works for my purposes. I am controlling 8 led lights with sliders so they can individually adjust 0-255. This will be used in an architectural model, and during the lectures, the user will be able to control the illumination of each room individually using sliders on the app. This code runs perfect on the uno (with the exception of 2 sliders acting as on/off instead of having pwm). I'm not quite sure about the "software serial" or what it does! I should also mention, I can control the lights using sketch, it just seems to be a bluetooth tx/rx issue. And the bluetooth is connected to the app (mid app inventor)
This code runs perfect on the uno (with the exception of 2 sliders acting as on/off instead of having pwm)
Thanks for the late clarification
TheMemberFormerlyKnownAsAWOL:
Remind me - which are the PWM pins on a Uno?
Why is your code so long?
the pwm pins on uno are 3,5,6,10,11. I should mention I altered the PIN numbers to be aligned with the MEGA, but none of my lights light up:(
I can control the lights using sketch, it just seems to be a bluetooth tx/rx issue.
How is the bluetooth module connected to the Mega? How was it connected to the UNO?
Which pin to which pin?
So I'm using the same 0,1 pins on each. As a matter of fact, they look the same on both boards. I am also beginning to find other with issues regarding software serial and uart. That being said, I'm not quite sure what that means, besides I may need to remove my "softwareserial" statements. That also being said, not quite sure what to put in their place.
You should certainly use Serial1 instead of software serial on the mega, but if things worked on the uno, they should work on the mega.
The module should be disconnected when the code uploads to the board. Then connection to hardware serial should be OK. What pin on the module is connected to pin0 and what pin is connected to pin1?
cattledog:
You should certainly use Serial1 instead of software serial on the mega, but if things worked on the uno, they should work on the mega.
The module should be disconnected when the code uploads to the board. Then connection to hardware serial should be OK. What pin on the module is connected to pin0 and what pin is connected to pin1?
Yes. I made sure the board was disconnected when uploading the code. The pin 0 is connected to tx on the bt module, and rx to the 1 pin. But I have tried the opposite as well. several times.
The code you had for the UNO was strange in that it used software serial on the hardware serial pins, and I'm surprised that it actually worked. Software serial uses pin change interrupts, and the Mega does not support them on all pins. They are not supported on D0 and D1.
The Mega has multiple hardware serial ports, and you should use one of them. I would put the module on Serial1. Connect module TX to MegaRX Pin 19 and module RX to MegaTX pin 18.
//#include<SoftwareSerial.h>
//SoftwareSerial future(0, 1);
int led1 = 4;
int led2 = 5;
int led3 = 6;
int led4 = 7;
int led5 = 8;
int led6 = 9;
int led7 = 10;
int led8 = 11;
void setup() {
Serial1.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
pinMode(led8, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial1.available()) {
int value1 = Serial1.parseInt();
int value2 = Serial1.parseInt();
int value3 = Serial1.parseInt();
int value4 = Serial1.parseInt();
int value5 = Serial1.parseInt();
int value6 = Serial1.parseInt();
int value7 = Serial1.parseInt();
int value8 = Serial1.parseInt();
if (Serial1.read() == '\n') {
analogWrite(led1, value1);
analogWrite(led2, value2);
analogWrite(led3, value3);
analogWrite(led4, value4);
analogWrite(led5, value5);
analogWrite(led6, value6);
analogWrite(led7, value7);
analogWrite(led8, value8);
}
}
}
cattledog:
The code you had for the UNO was strange in that it used software serial on the hardware serial pins, and I'm surprised that it actually worked. Software serial uses pin change interrupts, and the Mega does not support them on all pins. They are not supported on D0 and D1.
The Mega has multiple hardware serial ports, and you should use one of them. I would put the module on Serial1. Connect module TX to MegaRX Pin 19 and module RX to MegaTX pin 18.
//#include<SoftwareSerial.h>
//SoftwareSerial future(0, 1);
int led1 = 4;
int led2 = 5;
int led3 = 6;
int led4 = 7;
int led5 = 8;
int led6 = 9;
int led7 = 10;
int led8 = 11;
void setup() {
Serial1.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
pinMode(led8, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial1.available()) {
int value1 = Serial1.parseInt();
int value2 = Serial1.parseInt();
int value3 = Serial1.parseInt();
int value4 = Serial1.parseInt();
int value5 = Serial1.parseInt();
int value6 = Serial1.parseInt();
int value7 = Serial1.parseInt();
int value8 = Serial1.parseInt();
if (Serial1.read() == '\n') {
analogWrite(led1, value1);
analogWrite(led2, value2);
analogWrite(led3, value3);
analogWrite(led4, value4);
analogWrite(led5, value5);
analogWrite(led6, value6);
analogWrite(led7, value7);
analogWrite(led8, value8);
}
}
}
I mean this honestly, can I send you some cookies or money. It works!!Thanks a ton!