Hello I have a simple sketch running on leanardo. The problem is that the serial print going to the xbee is not working immediately, IE its in a loop and on the first 15-60 iterations it wont send anything. However when i run a sketch with just serial print for xbee it prints every iteration with no delay added.
the problem sketch initializes a few ints and a very small string. In the void setup, I turn on a couple LEDS set one pwm pin to ground and read a pin. All analogs are in use. I tried turning on/off an led just before the iterations and it works. the code looks something like this
loop
{
serial.print
analog on
delay
analog off
delay
}
when i put another loop before what is above it will atleast start sending the serial at some point before it ends. Then when it ends it always prints the iterations in the subsequent loop. but above the loop like the Serial prints wont work. So that is my solution for now.
I found this post to be informative on SRAM, however i really dont think that is the issue here. Im just printing a single char as well.
What puzzles me is that if you have a simple sketch you don't just post it. Instead you retype it in some vague mish-mash of pseudo code, which we can't possibly analyze.
#include <SoftwareSerial.h>
//blue-RX,white-Tx,yellow-pwr,green-grnd
SoftwareSerial mySerial(8,2); //(RX,TX)
char b = 'L';
int c=0;
void setup() {
// set the data rate fZor the SoftwareSerial port
mySerial.begin(57600);
delay(1000);
//Serial.println("Hello, world?");
}
void loop()
{
while (c<100){
mySerial.print(b);
mySerial.print(c);
c+=1;
}
}
and the accompanying log but its in hexadecimal, feel free to translate if you dont think L0 printed followed by L1...L2...:
Even a simple array like the one below uses 2000 bytes of memory. This line alone would almost certainly make your program crash:
Code: [Select]
int myData [20] [50];
that is 20x50 array?
I mean my script has like 10 ints and a char....? and like 5-8 conditionals and or while loops. Maybe the multiplication in the delay slows it as well. and maybe the random function. I think arduino is a weak microprocessor.
is there function that can calculate the SRAM that may be used in a script? like run script to get SRAM max. the while loops take SRAM? any information on how to calculate by hand? I assume the variable names like ve will lessen SRAM usage compared to variable name like ALPHABET_ENTIRE, is that correct?
I mean explain how it could work once and not work multiple times? and i didnt change enough to make a case for too much SRAM being used, unless sometimes it works even at max, are we at max?
WAIT HERE IS THE SECOND ITERATION AFTER UNPLUGGING USB AND REPLUGING FOR POWER.
I have since changed the serial1.print() as it was from something before but its irrelevant. I changed it to print D. anyway this third log shows no T print after the X print and 100ms delay.
Will the delay allow for things to catch back up? is there function to clear SRAM?
I assume the variable names like ve will lessen SRAM usage compared to variable name like ALPHABET_ENTIRE, is that correct?
No, you can use very long names if you want. The compiler does not upload the variable names to your board. It is better if the sketch is easy to read.
int ph=0;
int ve=0;
int t_s = analogRead(A4);
if (t_s > 400){analogWrite(A0,255);}
if (t_s <400){ph =1;ve=1;};
This is hard work, reading your sketch. Meaningful data names would be much better. And proper indentation. Don't cram a whole lot of stuff on one line, that doesn't save any memory.
This would be better:
int ph = 0;
int ve = 0;
int t_s = analogRead(A4);
if (t_s > 400)
{
analogWrite(A0, 255);
}
if (t_s < 400)
{
ph = 1;
ve = 1;
}
Better still would be data names that mean something.
Thanks for the suggestion. Do you really think that is relevant? Does the exchange of an if for an else clear enough SRAM for my sketch to run more smoothly?
This is why I wrote the psuedo code to begin with because its just really a few lines and some loops.
I can't make any sense of your code, or what is happening to you. I guess no-one else can either. Please post some code, state what you expect to happen, and what actually happens.
Your code in reply #9 doesn't compile. You have to post code that compiles. How can we verify anything if you leave chunks out?
You construct SoftwareSerial ms and than print to Serial1 on Leonardo.
Is that OK? Just asking.
The other "problem " is your code is not self-documenting and when you use conditions such as if( C> 3456) how do you know if each c has no delay of several seconds inserted somewhere in the code?
Maybe that is the issue.
So how about next time:
if(ChickenCount > 3456 ) // too many chickens in the yard
Arduino to Arduino is not working. Arduino to xbee usb dongle/xctu is working. In the past I have got arduino to arduino to work.
Here is transmit code:
//#include <SoftwareSerial.h>
//blue-RX,white-Tx,yellow-pwr,green-grnd
//SoftwareSerial mySerial(8,2); //(RX,TX)
char b = 'A';
int c=0;
int d=0;
void setup()
{
// set the data rate fZor the SoftwareSerial port
Serial1.begin(9600);
//debug light on
analogWrite(A0,255);
delay(20);
delay(1000);
//Serial.println("Hello, world?");
}
void loop()
{
//analogWrite(A0,255);
c=0;
while (c<80){
Serial1.print(b);
Serial1.print(c);
c+=1;
}
delay(100);
Serial1.print('A');
d=0;
while(d<5){Serial1.print('A');
delay(5);
d+=1;}
analogWrite(A0,0);
delay(3000);
}
Here is receive code:
//#include <SoftwareSerial.h>
//blue-RX,white-Tx,yellow-pwr,green-grnd
//SoftwareSerial mySerial(8,2); //(RX,TX)
//variables some of which like message are irrelevant
char message[3]="GK";
char a = 'a';
char b = 'a';
char c ='a';
char d = 'a';
char o = 'O';
void setup() {
// set the data rate RF
Serial1.begin(9600);
delay(1200);
//set data rate for arduino IDE
Serial.begin(9600);
delay(2000);
//analogWrite(A0,255);
//Serial.println("Hello, world?");
}
void loop()
{
if (Serial1.available()){
b=Serial1.read();
Serial.println("b is:");
Serial.println(b);
}
}