Go Down

### Topic: need help (Read 2568 times)previous topic - next topic

#### pittom

##### Feb 05, 2011, 10:39 pm
Hi,i just started learning how to write code and don't know where i am going wrong.I do good until i add 4 inputs,what do you all think
thanks tom

/*

4 inputs with with (and &&) and 2 outputs

*/

// set pin numbers:
int buttonPin1 = 2;   /*come in from rovio
int buttonPin2 = 3;    *
int buttonPin3 = 4;    *
int buttonPin4 = 5;   */
int ledPin =  10;      // out to wheels
int ledpin =  8;       //out to wheels
// variables will change:
int buttonState1 = 0;         /* variable for reading rovio status
int buttonState2 = 0;          *
int buttonState3 = 0;          *
int buttonState4 = 0;          */
void setup() {
// initialize the pin as an output and input
pinMode(10, OUTPUT);
pinMode(8, OUTPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(2, INPUT);
}

void loop(){

// if it is, then it is HIGH:
if (buttonState1 == HIGH && buttonState2 == HIGH){

// turn LED on:
digitalWrite(ledPin = 10, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin = 10, LOW);
}

if (buttonState3 == HIGH && buttonState4 == HIGH) {
// turn LED on:
digitalWrite(ledPin = 8, HIGH); //turn led on
}
else {

digitalWrite(ledPin = 8, LOW);
}
}

#1
##### Feb 06, 2011, 12:45 am
How are buttons wired up?
Or are they not really buttons, but 0/5V output from the rovio?

Fix these type of statements:
// turn LED on:
digitalWrite(ledPin = 10, HIGH);  << do not add the ' =10' here

You gave all the pins names, why not use them here?
pinMode(10, OUTPUT);
pinMode(8, OUTPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(2, INPUT);

also you could make your output pin names more clear:
ledPin  vs ledpin
this will help to prevent this:
// turn LED on:
digitalWrite(ledPin = 10, HIGH);
& later on
digitalWrite(ledPin = 8, HIGH);

But back to the basic question - what is driving the buttons? Is it an actively driven signal, or a button with a pullup or pulldown resistor that then needs debouncing?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### pittom

#2
##### Feb 06, 2011, 12:58 am
it is o/5volt (low/high) coming out of rovio.I am just learning how to do this,i can program ladder logic very well but this is all new.i am not sure what this means i will look it up   << do not add the ' =10' here
as for names i was trying to keep it simple while i am learning. thanks for help any more advise will be very helpful
tom

#### retrolefty

#3
##### Feb 06, 2011, 01:13 am

digitalWrite(ledPin = 10, HIGH);

This is bad style. You should use:  digitalWrite(10, HIGH);

Or better still, use a named pin: #define launchPin = 10;

You could then use:   digitalWrite(launchPin, HIGH);

This latter example is the best in case you ever have to change pin wiring you only have to update your sketch at the define line and all the statements that use that pin name will automatically track that change when you recompile.

Lefty

#4
##### Feb 06, 2011, 01:38 amLast Edit: Feb 06, 2011, 02:49 am by CrossRoads Reason: 1
Make these changes to help prevent future confusion:
int ledPin10 =  10;      // out to wheels
int ledPin8 =  8;       //out to wheels

Similarly, change these:
pinMode(10, OUTPUT);  --> pinMode(ledPin10, OUTPUT);
pinMode(8, OUTPUT); --> pinMode (ledPin8, OUTPUT);
pinMode(3, INPUT);  --> pinMode (buttonPin3, INPUT);
pinMode(4, INPUT); --> pinMode (buttonPin4, INPUT);
pinMode(5, INPUT); --> pinMode (buttonPin5, INPUT);
pinMode(2, INPUT);  --> pinMode (buttonPin2, INPUT);

and then change the names here to match:
// turn LED on:
digitalWrite(ledPin = 10, HIGH); --> ledPin10, HIGH
}
else {
// turn LED off:
digitalWrite(ledPin = 10, LOW);  --> ledPin10, LOW
}

if (buttonState3 == HIGH && buttonState4 == HIGH) {
// turn LED on:
digitalWrite(ledPin = 8, HIGH); //turn led on  --> ledPin8, HIGH
}
else {
digitalWrite(ledPin = 8, LOW);  --> ledPin8, LOW
}

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### retrolefty

#5
##### Feb 06, 2011, 02:41 am
CR....yellow text on light grey background just doesn't work....

#6
##### Feb 06, 2011, 02:46 amLast Edit: Feb 06, 2011, 02:56 am by CrossRoads Reason: 1
Sorry, was trying to highlight, got the wrong thing I guess. Let me see if I can edit.

no more [highlight] test [/highlight] ??
red glow  this just looks blurry

We need the highlight option back.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### pittom

#7
##### Feb 06, 2011, 03:28 am
ok i still get error here is the new one

/*

4 inputs with with (and &&) and 2 outputs

*/

// set pin numbers:
int rredPin1 = 2;      /*come in from rovio
int rblackPin2 = 3;     *
int lredPin3 = 4;       *
int lblackPin4 = 5;     *
int fwheelPin =  10;      // out to wheels
int rwheelpin =  8;
// variables will change:
int rredState1 = 0;         /* variable for reading rovio status
int rblackState2 = 0;        *
int lredState3 = 0;          *
int lblackState4 = 0;        */
void setup() {
// initialize the pin as an output and input
pinMode(10, OUTPUT);
pinMode(8, OUTPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(2, INPUT);
}

void loop(){

// if it is, then it is high
if (rredState1 == HIGH && rblackState2 == HIGH){

// turn LED on:
digitalWrite(fwheelPin, HIGH);
}
else {
// turn LED off:
digitalWrite(fwheelPin1, LOW);
}

if (lredState3 == HIGH && lblackState4 == HIGH) {
// turn LED on:
digitalWrite(rwheelPin, HIGH);
}
else {

digitalWrite(rwheelPin, LOW);
}
}

bobo_bot_wont_work.cpp: In function 'void loop()':
bobo_bot_wont_work:32: error: 'rredState1' was not declared in this scope
bobo_bot_wont_work:33: error: 'rblackState2' was not declared in this scope
bobo_bot_wont_work:33: error: 'rblackPin2' was not declared in this scope
bobo_bot_wont_work:34: error: 'lredState3' was not declared in this scope
bobo_bot_wont_work:34: error: 'lredPin3' was not declared in this scope
bobo_bot_wont_work:35: error: 'lblackState4' was not declared in this scope
bobo_bot_wont_work:35: error: 'lblackPin4' was not declared in this scope
bobo_bot_wont_work:41: error: 'fwheelPin' was not declared in this scope
bobo_bot_wont_work:45: error: 'fwheelPin1' was not declared in this scope
bobo_bot_wont_work:52: error: 'rwheelPin' was not declared in this scope
bobo_bot_wont_work:56: error: 'rwheelPin' was not declared in this scope

#8
##### Feb 06, 2011, 03:44 am
the line where you have
/*
everything between that and
*/
is considered a comment.
Fix that stuff up.
If you  want a comment on an indivual line, put // and then your comment
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### pittom

#9
##### Feb 06, 2011, 03:45 am
ok found one fix /* insted of //* but now cnat find fix for
56: error: 'rwheelPin' was not declared in this scope

#10
##### Feb 06, 2011, 04:02 am
rwheelpin is a different variable than this rwheelPin, make them match

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### pittom

#11
##### Feb 06, 2011, 03:27 pm
thank for all your help it workes now.Like i said i use to program robots in a dvd factory with ladder logic and flow charts when i was young now i am old have to learn all over a new way to have fun with my toys,it not all mechanical any more