Code is compiling without any error but the project is not working

i have 2 pushbuttons a dual relay and a motor (driven by relay).
i need pushbutton1 to close relay1(only for 0.5 sec) and after 0.5 sec close relay2 (for 0.5 sec)
and pushbutton2 to do the opposite
its compiling but not working
thanks in advance

// set pin numbers:
const int buttonPin1 = 2;     // the number of the pushbutton pin 2
const int buttonPin2 = 3;     // the number of the pushbutton pin 3
const int ledPin =  13;      //  the number of the LED pin
const int gearup = 7;        //  the number of the gearup (rotating clockwise)
const int geardown = 8;      //  the number of the geardown pin (rotating counter clockwise)

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;


void setup() {
 // initialize the LED pin as an output:
 pinMode(ledPin, OUTPUT);    
 // initialize the gearup as an input:
 pinMode(gearup, OUTPUT);  
 // initialize the geardown as an input:
 pinMode(geardown, OUTPUT);
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin1, INPUT);
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin2, INPUT);    

 digitalWrite(buttonPin1, HIGH);
 digitalWrite(buttonPin2, HIGH);

 digitalWrite(gearup, HIGH);
 digitalWrite(geardown, HIGH);
 Serial.begin(9600);
}

void loop(){ // put your main code here, to run repeatedly:
  digitalWrite(buttonPin1, HIGH);
  digitalWrite(gearup, HIGH);
  digitalWrite(geardown, HIGH);
  delay(500);
  Serial.println("buttonPin1 ON");
  
  
  digitalWrite(buttonPin2, HIGH);
  digitalWrite(geardown, HIGH);
  digitalWrite(gearup, HIGH);
  delay(500);
  Serial.println("buttonPin2 OFF");
}

Shouldn't you be reading the switches, not writing?

thanks!!!

I suspect 98% of programs written can be compiled and don't work :slight_smile:

...R

Chaser:

 // initialize the pushbutton pin as an input:

pinMode(buttonPin1, INPUT);
digitalWrite(buttonPin1, HIGH);

This can be simplified and made more obvious like this:

 pinMode(buttonPin1, INPUT_PULLUP);

Also I miss if statements. Even if you change that to a digitalRead(), nothing is done with the reading.

Furthermore you never switch off the relays. This code (assuming wired correctly) will immediately switch on relay1, and 0.5 seconds later relay2, after which they remain in that state forever.

Nothing new happens to the gearup and geardown pins either, after they're set HIGH in setup().

Important lesson:-
When a code compiles it means that the individual instructions make sense to the computer. It is the sum total of all the individual instructions working sequentially that makes the code do something. Any compiled code will do something but whether that something is what the writer of the code intended is another matter.

Most of program development consists in getting the right sequence of instructions to do what you want.

In this respect C/C++ allows you a lot of flexibility in what you write assuming you know what you are doing. Other languages like Pascal, or Python hide stuff from you and attempt to constrain what you can write but making it harder to write rubbish.

Trust me, Python is a perfectly suitable language to write absolute rubbish in.

wvmarle:
Trust me, Python is a perfectly suitable language to write absolute rubbish in.

:slight_smile: :slight_smile:
Yes I know but my big bugbear with it is it hides variable types from you, and just guesses at what you want them to be, sometimes changing that guess as you go along, leading to very puzzling error messages.

wvmarle:
Trust me, Python is a perfectly suitable language to write absolute rubbish in.

It's no better or worse than any other computer language. The rubbish always comes from the person at the keyboard.

However IMHO Python (or Ruby etc) make programming a bit easier for newbies compared with C/C++

...R

It does save you quite some head banging over runtime crashes caused due to having a char array declared one character too short!

Robin2:
However IMHO Python (or Ruby etc) make programming a bit easier for newbies compared with C/C++

Sure it's easier. And it's so easy to create a nice looking Gnome UI using Glade and Python and PyGTK - I've written quite complex things within a few hours.
OTOH many things you don on Arduino are just not possible in Python or other such interpreted, such as the direct hardware access, accurately timing interrupts, etc.