Pages: [1]   Go Down
Author Topic: Please help with debugging...  (Read 529 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I am trying to combine two sketches:

1. Arduino shift out tutorial: http://arduino.cc/en/Tutorial/ShiftOut

2. Arduino keypad 4x4 tutorial: http://learning.grobotronics.com/arduino-keypad-4x4-en.html

It gives few error messages which are a mystery to me, like:


sketch_jul07a:55: error: expected unqualified-id before 'if'
sketch_jul07a:56: error: expected unqualified-id before '{' token

Please help me to debug this...thanks in advance.



Code:
#include <Keypad.h>
/*
  Shift Register Example
 for two 74HC595 shift registers

 This sketch turns on each of the LEDs attached to two 74HC595 shift registers,
 in sequence from output 0 to output 15.

 Hardware:
 * 2 74HC595 shift register attached to pins 2, 3, and 4 of the Arduino,
 as detailed below.
 * LEDs attached to each of the outputs of the shift register

 Created 22 May 2009
 Modified 23 Mar 2010
 by Tom Igoe
}
 */
//Pin connected to latch pin (ST_CP) of 74HC595


const int latchPin = 8;

//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 11;
char inputString[2];


const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] =
  {{'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}};

byte rowPins[ROWS] = {2,3,4,5}; //connect to row pinouts
byte colPins[COLS] = {6,7,8,9}; //connect to column pinouts
int count=0;

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){

 //set pins to output because they are addressed in the main loop
 pinMode(latchPin, OUTPUT);
 pinMode(dataPin, OUTPUT); 
 pinMode(clockPin, OUTPUT);
 Serial.begin(9600);}
 
void loop();

  char key = keypad.getKey();
if (key != NO_KEY);
{
  Serial.print(key);
count++;
   if (count==250)
    {
      count=0;
    }
  }



// iterate over the 16 outputs of the two shift registers
  for (int thisLed = 0; thisLed < 16; thisLed++) {
    // write data to the shift registers:
    registerWrite(thisLed, HIGH);
    // if this is not the first LED, turn off the previous LED:
    if (thisLed > 0) {
      registerWrite(thisLed - 1, LOW);
    }
    // if this is  the first LED, turn off the highest LED:
    else {
      registerWrite(15, LOW);
    }
  delay(key);
 
  }



// This method sends bits to the shift registers:


 
  // the bits you want to send. Use an unsigned int,
  // so you can use all 16 bits:
  unsigned int bitsToSend = 0;   

  // turn off the output so the pins don't light up
  // while you're shifting bits:
  digitalWrite(latchPin, LOW);

  // turn on the next highest bit in bitsToSend:
  bitWrite(bitsToSend, whichPin, whichState);

  // break the bits into two bytes, one for
  // the first register and one for the second:
  byte registerOne = highByte(bitsToSend);
  byte registerTwo = lowByte(bitsToSend);

  // shift the bytes out:
  shiftOut(dataPin, clockPin, MSBFIRST, registerTwo);
  shiftOut(dataPin, clockPin, MSBFIRST, registerOne);

  // turn on the output so the LEDs can light up:
  digitalWrite(latchPin, HIGH);
}
Logged

New England
Offline Offline
Sr. Member
****
Karma: 7
Posts: 295
Natural Semiinductor
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Syntax error
void loop();
  char key = keypad.getKey();

syntax style needed

void loop()
{
  unsigned int modulus = slow_speed;     // it determines how often
$$$$$$$$$$$$$$$$$$$
delete ;
add {
}
Logged

I am going to get going.

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 90
Posts: 3527
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The error that you quote is caused by the semicolon at the end of this line
Code:
if (key != NO_KEY);

Remove that and the error will go away.  Unfortunately there are other errors in the code, including (yet) another spurious semicolon at the end of this line
Code:
void loop();
and the fact that the loop() function does not have an opening brace.

Fix those and there are still problems though.  Can you explain what the program is supposed to do ?  The shift register portion of the program seems to have no connection with the keypad portion, and several function and variable declarations are missing.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!

Many thanks for your tips. However there is still something deeply wrong with my coding.

I expect this program to blink the knight rider lights according to rhythm which I set with a keypad. For example when the light are blinking with  a knight rider style I may press from the keypad with the numbers "123" and the delay between the blinking lights will be 123 milliseconds. However today I received a lcd screen for the Arduino and I would like to add a visual feedback for the user. But thats another story, lets try to make this code work as is before making things truly complex.

The code is now like this and it gives the following errors:

sketch_jul07a:68: error: expected unqualified-id before 'for'
sketch_jul07a:68: error: expected constructor, destructor, or type conversion before '<' token
sketch_jul07a:68: error: expected constructor, destructor, or type conversion before '++' token


Code:
#include <Keypad.h>
/*
  Shift Register Example
 for two 74HC595 shift registers

 This sketch turns on each of the LEDs attached to two 74HC595 shift registers,
 in sequence from output 0 to output 15.

 Hardware:
 * 2 74HC595 shift register attached to pins 2, 3, and 4 of the Arduino,
 as detailed below.
 * LEDs attached to each of the outputs of the shift register

 Created 22 May 2009
 Modified 23 Mar 2010
 by Tom Igoe
}
 */
//Pin connected to latch pin (ST_CP) of 74HC595


const int latchPin = 8;

//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 11;
char inputString[2];


const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] =
  {{'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}};

byte rowPins[ROWS] = {2,3,4,5}; //connect to row pinouts
byte colPins[COLS] = {6,7,8,9}; //connect to column pinouts
int count=0;

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){

 //set pins to output because they are addressed in the main loop
 pinMode(latchPin, OUTPUT);
 pinMode(dataPin, OUTPUT); 
 pinMode(clockPin, OUTPUT);
 Serial.begin(9600);}
 
void loop()
{
  char key = keypad.getKey();
if (key != NO_KEY);

  Serial.print(key);
count++;
   if (count==250)
    {
      count=0;
    }
  }



// iterate over the 16 outputs of the two shift registers
  for (int thisLed = 0; thisLed < 16; thisLed++) {
    // write data to the shift registers:
    registerWrite(thisLed, HIGH);
    // if this is not the first LED, turn off the previous LED:
    if (thisLed > 0) {
      registerWrite(thisLed - 1, LOW);
    }
    // if this is  the first LED, turn off the highest LED:
    else {
      registerWrite(15, LOW);
    }
  delay(key);
 
  }



// This method sends bits to the shift registers:


 
  // the bits you want to send. Use an unsigned int,
  // so you can use all 16 bits:
  unsigned int bitsToSend = 0;   

  // turn off the output so the pins don't light up
  // while you're shifting bits:
  digitalWrite(latchPin, LOW);

  // turn on the next highest bit in bitsToSend:
  bitWrite(bitsToSend, whichPin, whichState);

  // break the bits into two bytes, one for
  // the first register and one for the second:
  byte registerOne = highByte(bitsToSend);
  byte registerTwo = lowByte(bitsToSend);

  // shift the bytes out:
  shiftOut(dataPin, clockPin, MSBFIRST, registerTwo);
  shiftOut(dataPin, clockPin, MSBFIRST, registerOne);

  // turn on the output so the LEDs can light up:
  digitalWrite(latchPin, HIGH);
}
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (key != NO_KEY);

  Serial.print(key);
count++;
   if (count==250)
    {
      count=0;
    }
  }

Rarely is it a good idea to put a semicolon at the end of an if statement. Also, count the number of the left braces versus the number of right braces.
« Last Edit: July 08, 2013, 08:59:55 pm by Arrch » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello again,

Please be specific cos I am rather new to Arduino coding. I hope somebody can help me with this...

Anyway the compiler now gives the following errors:

 
sketch_jul07a:66: error: expected unqualified-id before 'for'
sketch_jul07a:66: error: expected constructor, destructor, or type conversion before '++' token

With the following code:




Code:
#include <Keypad.h>
/*
  Shift Register Example
 for two 74HC595 shift registers

 This sketch turns on each of the LEDs attached to two 74HC595 shift registers,
 in sequence from output 0 to output 15.

 Hardware:
 * 2 74HC595 shift register attached to pins 2, 3, and 4 of the Arduino,
 as detailed below.
 * LEDs attached to each of the outputs of the shift register

 Created 22 May 2009
 Modified 23 Mar 2010
 by Tom Igoe
}
 */
//Pin connected to latch pin (ST_CP) of 74HC595


const int latchPin = 8;

//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 11;
char inputString[2];


const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] =
  {{'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}};

byte rowPins[ROWS] = {2,3,4,5}; //connect to row pinouts
byte colPins[COLS] = {6,7,8,9}; //connect to column pinouts
int count=0;

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){

 //set pins to output because they are addressed in the main loop
 pinMode(latchPin, OUTPUT);
 pinMode(dataPin, OUTPUT); 
 pinMode(clockPin, OUTPUT);
 Serial.begin(9600);}
 
void loop(){
char key = keypad.getKey();
if (key != NO_KEY)
{
  Serial.print(key);
count++;
   if (count==250)
    {
      count=0;
    }
  }
}

// iterate over the 16 outputs of the two shift registers
  for (int thisLed = 0 thisLed < 16; thisLed++) {
    // write data to the shift registers:
    registerWrite(thisLed, HIGH);
    // if this is not the first LED, turn off the previous LED:
    if (thisLed > 0) {
      registerWrite(thisLed - 1, LOW);
    }
    // if this is  the first LED, turn off the highest LED:
    else {
      registerWrite(15, LOW);
    }
  delay(key);
 
  }



// This method sends bits to the shift registers:


 
  // the bits you want to send. Use an unsigned int,
  // so you can use all 16 bits:
  unsigned int bitsToSend = 0;   

  // turn off the output so the pins don't light up
  // while you're shifting bits:
  digitalWrite(latchPin, LOW);

  // turn on the next highest bit in bitsToSend:
  bitWrite(bitsToSend, whichPin, whichState);

  // break the bits into two bytes, one for
  // the first register and one for the second:
  byte registerOne = highByte(bitsToSend);
  byte registerTwo = lowByte(bitsToSend);

  // shift the bytes out:
  shiftOut(dataPin, clockPin, MSBFIRST, registerTwo);
  shiftOut(dataPin, clockPin, MSBFIRST, registerOne);

  // turn on the output so the LEDs can light up:
  digitalWrite(latchPin, HIGH);
Logged

Offline Offline
Edison Member
*
Karma: 56
Posts: 2189
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
sketch_jul07a:66: error: expected unqualified-id before 'for'
sketch_jul07a:66: error: expected constructor, destructor, or type conversion before '++' token

Line 66 has an ERROR in it.
Look up how to use  the "for"(.....)  syntax.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I really do not understand what is wrong with the line 66...

I do have searched all the tutorials...
Logged

Offline Offline
Edison Member
*
Karma: 56
Posts: 2189
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

See below:


* 7-8-2013 7-20-53 PM.jpg (24.95 KB, 481x149 - viewed 18 times.)
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 25
Posts: 1883
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There is only one way to combine sketches

1. Understand what the first sketch does and why it does it.

2. Understand what the second sketch does and why it does it.

3 Write a new sketch that does what you want!

Mark
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok. Many thanks Larry D.

Now the compiler complains only one error:

sketch_jul07a:69: error: expected unqualified-id before 'for'

this happens at the line 66...

holmes4. I am asking help in order to combine these two sketches. I think you are very right but I was wishing to get quickly decent results.

As said I am very new to programming so please be patient with me. After all I am learning all the time I think.
Logged

Offline Offline
Edison Member
*
Karma: 56
Posts: 2189
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

See below:


* 7-8-2013 8-31-54 PM.jpg (35.99 KB, 464x301 - viewed 28 times.)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46291
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you'd put each { on a new line (where they belong) and learn to use Tools + Auto Format, you wouldn't be having half the problems you are having now.
Logged

Pages: [1]   Go Up
Jump to: