Hi, i have these error messages and wondering what i have done wrong, and how to change it,
thanks for any help!
int latchPin = 8; //Pin connected to Pin 12 of 74HC595 (Latch)
int clockPin = 12; //Pin connected to Pin 11 of 74HC595 (Clock)
int dataPin = 11; //Pin connected to Pin 14 of 74HC595 (Data)
void setup() {
//set pins to output
}
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
void loop() {
for (int i = 0; i < 256; i++) { //count from 0 to 255
} }
digitalWrite(latchPin, LOW); //set latchPin low to allow data flow
shiftOut(i);
shiftOut(255-i);
//set latchPin to high to lock and send data
digitalWrite(latchPin, HIGH);
delay(250 );
void shiftOut(byte dataOut) {
boolean pinState; // Shift out 8 bits LSB first, on rising edge of clock
digitalWrite(dataPin, LOW); //clear shift register ready for sending data
digitalWrite(clockPin, LOW);
for (int i=0; i<=7; i++) { // for each bit in dataOut send out a bit
digitalWrite(clockPin, LOW); //set clockPin to LOW prior to sending bit
// if value of DataOut and (logical AND) a bitmask are true, set pinState to 1 (HIGH)
if ( dataOut & (1<<i) ) {
}
pinState = HIGH;
}
else {
}
pinState = LOW;
//sets dataPin to HIGH or LOW depending on pinState
digitalWrite(dataPin, pinState);
digitalWrite(clockPin, HIGH); //send bit out on rising edge of clock
digitalWrite(dataPin, LOW);
}
digitalWrite(clockPin, LOW); //stop shifting
Here are my errors:
Code.ino:7:8: error: expected constructor, destructor, or type conversion before '(' token
Code.ino:8:8: error: expected constructor, destructor, or type conversion before '(' token
Code.ino:9:8: error: expected constructor, destructor, or type conversion before '(' token
Code.ino:13:13: error: expected constructor, destructor, or type conversion before '(' token
Code.ino:14:9: error: expected constructor, destructor, or type conversion before '(' token
Code.ino:15:9: error: expected constructor, destructor, or type conversion before '(' token
Code.ino:17:13: error: expected constructor, destructor, or type conversion before '(' token
Code.ino:18:6: error: expected constructor, destructor, or type conversion before '(' token
Code.ino: In function 'void shiftOut(byte)':
Code.ino:30:1: error: 'else' without a previous 'if'
Code.ino: At global scope:
Code.ino:38:13: error: expected constructor, destructor, or type conversion before '(' token
Error compiling.
Code.ino:14:13: error: expected constructor, destructor, or type conversion before '(' token
Code.ino:15:9: error: expected constructor, destructor, or type conversion before '(' token
Code.ino:16:9: error: expected constructor, destructor, or type conversion before '(' token
Code.ino:18:13: error: expected constructor, destructor, or type conversion before '(' token
Code.ino:19:6: error: expected constructor, destructor, or type conversion before '(' token
Code.ino: In function 'void shiftOut(byte)':
Code.ino:31:1: error: 'else' without a previous 'if'
Code.ino: At global scope:
Code.ino:39:13: error: expected constructor, destructor, or type conversion before '(' token
Error compiling.
else {
}
pinState = LOW;
//sets dataPin to HIGH or LOW depending on pinState
digitalWrite(dataPin, pinState);
digitalWrite(clockPin, HIGH); //send bit out on rising edge of clock
digitalWrite(dataPin, LOW);
And here
void loop() {
for (int i = 0; i < 256; i++) { //count from 0 to 255
} }
Thanks, I’ve altered to my best ability the code from your comments,
int latchPin = 8; //Pin connected to Pin 12 of 74HC595 (Latch)
int clockPin = 12; //Pin connected to Pin 11 of 74HC595 (Clock)
int dataPin = 11; //Pin connected to Pin 14 of 74HC595 (Data)
void setup() {
//set pins to output
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop() {
for (int i = 0; i < 256; i++) { //count from 0 to 255
digitalWrite(latchPin, LOW); //set latchPin low to allow data flow
shiftOut(i);
shiftOut(255-i);
//set latchPin to high to lock and send data
digitalWrite(latchPin, HIGH);
delay(250 );
}}
void shiftOut(byte dataOut) {
boolean pinState; // Shift out 8 bits LSB first, on rising edge of clock
digitalWrite(dataPin, LOW); //clear shift register ready for sending data
digitalWrite(clockPin, LOW);
for (int i=0; i<=7; i++) { // for each bit in dataOut send out a bit
digitalWrite(clockPin, LOW); //set clockPin to LOW prior to sending bit
// if value of DataOut and (logical AND) a bitmask are true, set pinState to 1 (HIGH)
if ( dataOut & (1<<i) ) {
pinState = HIGH;
}}
else {
pinState = LOW;
//sets dataPin to HIGH or LOW depending on pinState
digitalWrite(dataPin, pinState);
digitalWrite(clockPin, HIGH); //send bit out on rising edge of clock
digitalWrite(dataPin, LOW);
digitalWrite(clockPin, LOW); //stop shifting
}}
But have this: (I think I may have not changed something right…)
Code.ino: In function 'void shiftOut(byte)':
Code.ino:33:1: error: 'else' without a previous 'if'
Error compiling.
I dont understand whats wrong with my if & else statement
int latchPin = 8; //Pin connected to Pin 12 of 74HC595 (Latch)
int clockPin = 12; //Pin connected to Pin 11 of 74HC595 (Clock)
int dataPin = 11; //Pin connected to Pin 14 of 74HC595 (Data)
void setup()
{
//set pins to output
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop()
{
for (int i = 0; i < 256; i++)
{ //count from 0 to 255
digitalWrite(latchPin, LOW); //set latchPin low to allow data flow
shiftOut(i);
shiftOut(255 - i);
//set latchPin to high to lock and send data
digitalWrite(latchPin, HIGH);
delay(250 );
}
}
void shiftOut(byte dataOut)
{
boolean pinState; // Shift out 8 bits LSB first, on rising edge of clock
digitalWrite(dataPin, LOW); //clear shift register ready for sending data
digitalWrite(clockPin, LOW);
for (int i = 0; i <= 7; i++)
{ // for each bit in dataOut send out a bit
digitalWrite(clockPin, LOW); //set clockPin to LOW prior to sending bit
// if value of DataOut and (logical AND) a bitmask are true, set pinState to 1 (HIGH)
if ( dataOut & (1 << i) )
{
pinState = HIGH;
}
}
else
{
pinState = LOW;
//sets dataPin to HIGH or LOW depending on pinState
digitalWrite(dataPin, pinState);
digitalWrite(clockPin, HIGH); //send bit out on rising edge of clock
digitalWrite(dataPin, LOW);
digitalWrite(clockPin, LOW); //stop shifting
}
}
Errors:
Code.ino: In function 'void shiftOut(byte)':
Code.ino:44:3: error: 'else' without a previous 'if'
Error compiling.
Make an attempt to use Auto Format.
If it does not work due to unmatched / mismatched code go thru each block of code and set up your cursor around each first bracket ( here { or here ) and see if the last MATCHING bracket is where you expect it.
This “block detector” is rather unreliable and if you combine it with mouse click it will also highlight the matching block.
It is better to start “inside out” to find the mismatched block.
Here is a good , original post , candidate.
if ( dataOut & (1 << i) )
{
block of code start
pinState = HIGH;
block of code end
}
} mismatch
else
{
block of code start
pinState = LOW;
//sets dataPin to HIGH or LOW depending on pinState
digitalWrite(dataPin, pinState);
digitalWrite(clockPin, HIGH); //send bit out on rising edge of clock
digitalWrite(dataPin, LOW);
digitalWrite(clockPin, LOW); //stop shifting
block of code end
}
}
Instead of trying to guess where the brackets go, or just putting them where you think they might go, please go look at some examples or tutorials on C++ and try to learn how they work so you'll KNOW where they need to go.