Problem with code: if (state! = buttonState)

Hello everyone!

I am working with an Arduino Uno as an effort to learn. I'm very new to Arduino, coding, electronics in general. I've already gone through all the preloaded sketches on the Arduino program and am looking for additional practice.

I'm trying to use a string of code I copied from a blog, which is supposed to control an LED with a pushbutton. I received a code error and would really appreciate help on this.

Thanks so much! :slight_smile:

The code is

// constants will not change 
const  int  buttonPin = 2; // the number of the push button pin 
const  int  ledPin = 13;   // the number of the LED pin
 
// variables wants change 
int  buttonState = 0;     // variable for reading the push button status 
int  ledState = 0;        // variable for reading the LED status
 
int  counter = 0;         // variable for remember the number of button pressed
 
 
// the setup routine runs once you turn the device on or you press reset 
void  setup ()
{
    // initialize the LED pin as an output 
    pinMode (ledPin,  OUTPUT );
 
    // initialize the pushbutton pins as input and enable internal pull-up resistor 
    pinMode (buttonPin,  INPUT_PULLUP );
 
    // initialize serial communication at 9600 bits per second
    Serial.begin (9600);
}
 
 
// the loop routine runs over and over again forever 
void  loop ()
{ 
    // read the state of the pushbutton value 
    int  state = digitalRead (buttonPin);
 
    // recognize state changes: button pressed and button released 
    if  (state! = buttonState)
    {
        // remember new button state
        buttonState = state;
 
        // print out the state of the button 
        Serial.print ( "2 State changed" );
        Serial.println (button state);
 
        // button is pressed and LED is off 
        if  (buttonState ==  LOW  && ledState ==  LOW )
        {
            // remember new LED state 
            ledState =  HIGH ;
 
            // turn LED on 
            digitalWrite (ledPin,  HIGH );
 
            // print out the state of the LED 
            Serial.print ( "2 set high" );
            Serial.println (ledState);
 
            // increment number of button pressed
            counter ++;
 
            // print out the number of button pressed 
            Serial.print ( "2 counter:" );
            Serial.println (counter);
        }
        // button is pressed and LED is on 
        else  if  (buttonState ==  LOW  && ledState ==  HIGH )
        {
            // remember new LED state 
            ledState =  LOW ;
 
            // turn LED off 
            digitalWrite (ledPin,  LOW );
 
            // print out the state of the LED 
            Serial.print ( "2 Set low" );
            Serial.println (ledState);
 
            // increment number of button pressed
            counter ++;
 
            // print out the number of button pressed 
            Serial.print ( "2 counter:" );
            Serial.println (counter);
        }
        // button is released 
        else  if  (buttonState ==  HIGH )
        {
            // print out new line
            Serial.println ();
 
            // wait before next click is recognized
            delay (100);
        }
    }
}

The error message is

Arduino: 1.8.5 (Mac OS X), Board: "Arduino/Genuino Uno"

Build options changed, rebuilding all
/Users/acyber/Documents/Arduino/Chriga_s_button_test/Chriga_s_button_test.ino: In function 'void loop()':
Chriga_s_button_test:32: error: expected ')' before '!' token
     if  (state! = buttonState)
               ^
Chriga_s_button_test:39: error: 'button' was not declared in this scope
         Serial.println (button state);
                         ^
exit status 1
expected ')' before '!' token

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

P.S. The blog can be found at the following site (it's in German but gets translated almost instantaneously) chriga's blog: Audino - Arduino MP3-Player (5) - Knöpfe (digital)

(deleted)

        Serial.println (button state);

'button state' and 'buttonState' aren't the same thing.

Pete

This will complie , check

// constants will not change
const  int  buttonPin = 2; // the number of the push button pin
const  int  ledPin = 13;   // the number of the LED pin
 
// variables wants change
int  buttonState = 0;     // variable for reading the push button status
int  ledState = 0;        // variable for reading the LED status
 
int  counter = 0;         // variable for remember the number of button pressed
 
 
// the setup routine runs once you turn the device on or you press reset
void  setup ()
{
    // initialize the LED pin as an output
    pinMode (ledPin,  OUTPUT );
 
    // initialize the pushbutton pins as input and enable internal pull-up resistor
    pinMode (buttonPin,  INPUT_PULLUP );
 
    // initialize serial communication at 9600 bits per second
    Serial.begin (9600);
}
 
 
// the loop routine runs over and over again forever
void  loop ()
{
    // read the state of the pushbutton value
    int  state = digitalRead (buttonPin);
 
    // recognize state changes: button pressed and button released
    if  (state != buttonState)
    {
        // remember new button state
        buttonState = state;
 
        // print out the state of the button
        Serial.print ( "2 State changed" );
        Serial.println (buttonState);
 
        // button is pressed and LED is off
        if  (buttonState ==  LOW  && ledState ==  LOW )
        {
            // remember new LED state
            ledState =  HIGH ;
 
            // turn LED on
            digitalWrite (ledPin,  HIGH );
 
            // print out the state of the LED
            Serial.print ( "2 set high" );
            Serial.println (ledState);
 
            // increment number of button pressed
            counter ++;
 
            // print out the number of button pressed
            Serial.print ( "2 counter:" );
            Serial.println (counter);
        }
        // button is pressed and LED is on
        else  if  (buttonState ==  LOW  && ledState ==  HIGH )
        {
            // remember new LED state
            ledState =  LOW ;
 
            // turn LED off
            digitalWrite (ledPin,  LOW );
 
            // print out the state of the LED
            Serial.print ( "2 Set low" );
            Serial.println (ledState);
 
            // increment number of button pressed
            counter ++;
 
            // print out the number of button pressed
            Serial.print ( "2 counter:" );
            Serial.println (counter);
        }
        // button is released
        else  if  (buttonState ==  HIGH )
        {
            // print out new line
            Serial.println ();
 
            // wait before next click is recognized
            delay (100);
        }
    }
}

Though I'm Curious what's the problem with this ?

spycatcher2k:

if  (state! = buttonState)

try

if  (state != buttonState)

Though I'm Curious what's the problem with this ?

The problem is the space between '!' and '='

Thanks so much everyone, it worked! I was nervous posting my first post, and your quick and helpful replies have made a really positive impression on me. Thanks again!