Show Posts
|
|
Pages: 1 [2]
|
|
18
|
Using Arduino / Storage / Re: Second time i read dir arduino halts
|
on: November 24, 2012, 04:04:18 pm
|
OK Here's the complete sketch: Trouble's near the bottom. /* mode 0: Menu mode 1: Toggle mode 2: SD Card mode 3: Serial input */
// include the library code: #include <ShiftRegLCD.h> #include <SoftwareSerial.h> #include <SD.h> #define rxPin 7 #define txPin 8 #define srdataPin 2 #define srclockPin 3
// set up variables using the SD utility library functions: Sd2Card card; SdVolume volume;
const int chipSelect = 10;
ShiftRegLCD lcd(srdataPin, srclockPin, TWO_WIRE, 4); SoftwareSerial mySerial(rxPin, txPin);
float x = 0.000; float y = 0.000; float z = 0.000; int latchPin = 4; int clockPin = 5; int dataPin = 6; int mode = 0; int menu_cursor = 1; byte arrow[8] = {16,24,28,30,28,24,16}; int serData; byte switchVar1 = 72; int btnYUp =1; int btnXLeft = 2; int btnXRight = 3; int btnYDown = 4; int btnForward = 5; int btnReverse = 6; int btnZDown = 7; int btnZUp= 8;
void setup() { // set up the LCD's number of columns and rows: lcd.createChar(0, arrow); //lcd.begin(40, 4); // Print a message to the LCD. pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, INPUT); // pinMode(rxPin, INPUT); // pinMode(txPin, OUTPUT); pinMode(10, OUTPUT); SD.begin(chipSelect); mainmenu(); }
void mainmenu() { lcd.setCursor(0,0); lcd.print("MENU"); lcd.setCursor(0,1); lcd.print(" Toggle mode"); lcd.setCursor(0,2); lcd.print(" SD Card"); lcd.setCursor(0,3); lcd.print(" Serial repeater"); lcd.setCursor(0,menu_cursor); lcd.print((char)0); }
// dir = 1 down dir=0 up void move_menu_cursor(int dir) { if (dir==1) { if (menu_cursor<3) { menu_cursor ++; mainmenu(); } } if (dir==2) { if (menu_cursor>1) { menu_cursor --; mainmenu(); } } }
void togglemode() { lcd.clear(); lcd.setCursor(0,0); lcd.print("Togglemode"); lcd.setCursor(0,1); lcd.print("X: 0.000"); lcd.setCursor(0,2); lcd.print("Y: 0.000"); lcd.setCursor(0,3); lcd.print("Z: 0.000"); while (mode==1) {
if (Button(btnZUp)) { while (Button(btnZUp)) {} z=z+0.100; } if (Button(btnZDown)) { while (Button(btnZDown)) {} z=z-0.100; } if (Button(btnYUp)) { while (Button(btnYUp)) {} y=y+0.100; } if (Button(btnYDown)) { while (Button(btnYDown)) {} y=y-0.100; } if (Button(btnXLeft)) { while (Button(btnXLeft)) {} x=x+0.100; } if (Button(btnXRight)) { while (Button(btnXRight)) {} x=x-0.100; } if (Button(btnReverse)) { while (Button(btnReverse)) {} mode=0; } lcd.setCursor(2,1); if (x>=0) {lcd.print(" ");} lcd.print(x); lcd.setCursor(2,2); if (y>=0) {lcd.print(" ");} lcd.print(y); lcd.setCursor(2,3); if (z>=0) {lcd.print(" ");} lcd.print(z); } }
void serialRepeater(){ lcd.clear(); lcd.setCursor(0,0); lcd.print("Serial repeater"); Serial.begin(9600); Serial.println("Ready ..."); mySerial.begin(9600); mySerial.listen(); while (mode==3) { while (mySerial.available()) Serial.write(mySerial.read()); while (Serial.available()) mySerial.write(Serial.read()); if (Button(btnReverse)) { while (Button(btnReverse)) {} mode=0; } } }
byte shiftIn(int myDataPin, int myClockPin) { int i; byte myDataIn = 0; pinMode(myClockPin, OUTPUT); pinMode(myDataPin, INPUT); for (i=0; i<8; i++) { digitalWrite(myClockPin, 0); delayMicroseconds(20); myDataIn = myDataIn << 1; myDataIn = myDataIn + digitalRead(myDataPin); digitalWrite(myClockPin, 1); } return myDataIn; }
boolean getBit(byte myVarIn, byte whatBit) { boolean bitState; bitState = myVarIn & (1 << (whatBit-1)); return bitState; }
boolean Button(byte b) { digitalWrite(latchPin,0); delayMicroseconds(20); digitalWrite(latchPin,1); switchVar1 = shiftIn(dataPin, clockPin); delayMicroseconds(20); return getBit(switchVar1, b); }
void sdCard() { lcd.clear(); int i=0; int j=0; String files [1][3]; File root; File entry; root = SD.open("/"); do { entry = root.openNextFile(); lcd.setCursor((j*10)+1, i); String ename=entry.name(); files[j][i]=ename; lcd.print(ename.substring(0,ename.indexOf("."))); i = i + 1; if ((i==4)&&(j==0)) {j=1; i=0;} } while ((entry)&&(i+j<5)); i=0; j=0; while (mode==2) { lcd.setCursor(j*10, i); lcd.print((char)0); if (Button(btnZDown)) { while (Button(btnZDown)) {} lcd.setCursor(j*10,i); lcd.print(" "); i = i + 1; if (i==4) { if (j==1) j=0; else j=1; i=0; } if (files[j][i]=="") {i=0; j=0;} } if (Button(btnReverse)) { while (Button(btnReverse)) {} mode=0; } } }
void loop() { if (Button(btnZDown)) { while (Button(btnZDown)) {} move_menu_cursor(1); } if (Button(btnZUp)) { while (Button(btnZUp)) {} move_menu_cursor(2); } if (Button(btnReverse)) { while (Button(btnReverse)) {} mode=menu_cursor; if (mode==1) togglemode(); if (mode==2) sdCard(); if (mode==3) serialRepeater(); lcd.clear(); mainmenu(); } }
|
|
|
|
|
19
|
Using Arduino / Storage / Second time i read dir arduino halts
|
on: November 24, 2012, 03:45:58 pm
|
I got my SC Card module working without problems. In my setup, I use a 4x20 LCD for a menu. In this menu when I select "SC Card", the following sub should run. And it does first time without any trouble - it shows names of 6 files on my SD Card. But second time it shows names for first 3 files and then get stuck. void sdCard() { lcd.clear(); int i=0; int j=0; String files [1][3]; File root; File entry; root = SD.open("/"); do { entry = root.openNextFile(); lcd.setCursor((j*10)+1, i); String ename=entry.name(); files[j][i]=ename; lcd.print(ename.substring(0,ename.indexOf("."))); i = i + 1; if ((i==4)&&(j==0)) {j=1; i=0;} } while ((entry)&&(i+j<5)); i=0; j=0; while (mode==2) { lcd.setCursor(j*10, i); lcd.print((char)0); if (Button(btnZDown)) { while (Button(btnZDown)) {} lcd.setCursor(j*10,i); lcd.print(" "); i = i + 1; if (i==4) { if (j==1) j=0; else j=1; i=0; } if (files[j][i]=="") {i=0; j=0;} } if (Button(btnReverse)) { while (Button(btnReverse)) {} mode=0; } } }
void loop() { if (Button(btnZDown)) { while (Button(btnZDown)) {} move_menu_cursor(1); } if (Button(btnZUp)) { while (Button(btnZUp)) {} move_menu_cursor(2); } if (Button(btnReverse)) { while (Button(btnReverse)) {} mode=menu_cursor; if (mode==1) togglemode(); if (mode==2) sdCard(); if (mode==3) serialRepeater(); lcd.clear(); mainmenu(); } }
|
|
|
|
|
20
|
Using Arduino / Networking, Protocols, and Devices / Re: Serial Repeater
|
on: November 23, 2012, 03:12:03 pm
|
|
@Pylon better in this context is that more data and fewer faults. There's approximately 400 bytes of data received by SoftwareSerial. In best cases I faults begins after 174 bytes. In worst cases I only receive 70 bytes. @robtillaart I have tried using different baud-rates, then I don't receive anything. I cant change the rate of the GRBL Arduino. If I use delays, I lose data. I seems that I got to collect the data from SoftwareSerial as quick as possible. I have tried to collect the data received by the SoftwareSerial connection in a String in my while loop and then write them back to the computer via Serial. That gave me only 100 bytes out of 400. My conclusion is that I got to buy a Mega end skip SoftwareSerial.
|
|
|
|
|
22
|
Using Arduino / Networking, Protocols, and Devices / Re: Serial Repeater
|
on: November 23, 2012, 10:41:57 am
|
There must be a more fundamental problem with SoftwareSerial. I made the tightest possible loop to see how much could be received flawlessly. This code while (1==1) { while (mySerial.available()) Serial.write(mySerial.read()); while (Serial.available()) mySerial.write(Serial.read()); };
is better than this code: while (1==1) { if (mySerial.available()) Serial.write(mySerial.read()); if (Serial.available()) mySerial.write(Serial.read()); };
Errors begin after approximately 175 characters. For example: $0 = 200.000 (steps/mm x) $1 = 200.000 (steps/mm y) $2 = 200.000 (steps/mm z) $3 = 50 (microseconds step pulse) $4 = 250.000 (mm/min default feed rate) $5 = 250.000 (mm/min default ee rae) $ =0.00(m/ac sgmnt 7 20 stp or ivet msk bnay 10000) 8 1.00 acelraioni m/se^2 9 005 (orerig untin evaton n m) $x=ale'tose praete o jst'$ t dmpcuren sttng k
Here the errors start at the line beginning with $5.
|
|
|
|
|
23
|
Using Arduino / Networking, Protocols, and Devices / Re: Serial Repeater
|
on: November 23, 2012, 04:38:53 am
|
Yes "while (!Serial) " is indeed the error. It must be an error I made when I was copying code from an example. I'm not used to program in C, so my eyes just didn't catch it. Now my only problem is, that when GRBL dump current settings, the text becomes more and more erroneous. Here's an example of the dump: $0 = 200.000 (steps/mm x) $1 = 200.000 (steps/mm y) $2 = 200.000 (steps/mm z) $3 = 50 (microseconds step pulse) $4 = 250.000 (mm/min default feed rate) $ = 5000 (m/indeaul sekrae) $ =0.00(m/ac sgmnt 7 20 ste prtiner msk bnay 100100 8 1.00 aceleatoninmmse^2 9 005 (crnrig untin dvitin n m) 'x=vlu' o etpaamte o jst'$'todup uren sttigs o
And the failures are not at the same positions each time.
|
|
|
|
|
24
|
Using Arduino / Networking, Protocols, and Devices / Re: Serial Repeater
|
on: November 22, 2012, 07:01:22 pm
|
Just got a hole through. I minimized the code. Now it looks like this: void serialRepeater(){ lcd.clear(); lcd.setCursor(0,0); lcd.print("Serial repeater"); Serial.begin(9600); Serial.println("Ready ..."); mySerial.begin(9600); mySerial.listen(); while (mode==3) { while (mySerial.available()){ Serial.write(mySerial.read()); } while (Serial.available()>0) { mySerial.write(Serial.read()); } if (Button(btnReverse)) { while (Button(btnReverse)) {} mode=0; } } lcd.clear(); mainmenu(); } Only problem now is, that it looses characters if message is long.
|
|
|
|
|
25
|
Using Arduino / Networking, Protocols, and Devices / Serial Repeater
|
on: November 22, 2012, 05:55:23 pm
|
I made a cnc where an Arduino is controlling the motors. It works very fine with GRBL flashed to the Arduino. It receives and send serial communication via USB to and from my PC. No problems. I would like to run my GRBL Arduino (uno rev3) from another Arduino (uno r2) – my control Arduino. The control Arduino should do a number of things but also be able to repeat serial communication from PC to GRBL Arduino. I connected GRBL Arduino's D0 and D1 to my Control Arduino D7 and D8 intending to use SoftwareSerial on these pins. rx->tx, tx->rx I can't make my Arduinos talk to each other. This is the parts of my code, which should do the job. What I sent from my PC is shown at the LCD. And the initial “Ready ...” arrives at my PC. So the loop is running OK. Can anybody figure out whats wrong. #include <SoftwareSerial.h> #define rxPin 7 #define txPin 8 ... SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin); ... void serialRepeater(){ lcd.clear(); lcd.setCursor(0,0); lcd.print("Serial repeater"); Serial.begin(9600); Serial.println("Ready ..."); while (!Serial) mySerial.begin(9600); mySerial.print("$"); while (mode==3) { if (mySerial.available()>0){ serData=mySerial.read(); Serial.write(serData); lcd.print(serData); } if (Serial.available()) { serData=Serial.read(); mySerial.write(serData); lcd.print(serData); Serial.write(serData); } if (Button(btnReverse)) { while (Button(btnReverse)) {} mode=0; } } lcd.clear(); mainmenu(); } void loop() { mode=3; serialRepeater(); }
|
|
|
|
|