-4

Can anyone give me the full code of my project because my code is not working?

My aim is that when the Z axis of an accelerometer is below 370 and a pushbutton connected to a pin 2 becomes high, then pin 13 turns high and remains high. When the Z axis is above 370 and pushbutton is high then the pin 13 goes low and remains low.

P.S I am also a noob to Arduino

Here is my code :

const int ledPin = 13;        
const int threshold = 370;    
const int pull_up_resistor = 12;
const int pull_down_resistor = 11;
const int groundpin = A1;              
const int powerpin = A5;            
const int xpin = A2;                   
const int ypin = A3;                  
const int zpin = A4;  

int pushButton = 2;


void setup() {

  pinMode(ledPin, OUTPUT);

  Serial.begin(9600);

  Serial.begin(9600);

  pinMode(pushButton, INPUT);
  digitalWrite(pushButton, HIGH);
  Serial.begin(9600);


  pinMode(groundpin, OUTPUT);
  pinMode(powerpin, OUTPUT);
  digitalWrite(groundpin, LOW);
  digitalWrite(powerpin, HIGH);
}


void loop() {

  int buttonState = digitalRead(pushButton);

  Serial.println(buttonState);
  delay(100);        // delay in between reads for stability

  Serial.print(analogRead(xpin));

  Serial.print("\t");
  delay(100);
  Serial.print(analogRead(ypin));

  Serial.print("\t");
  delay(100);
  Serial.print(analogRead(zpin));
  Serial.println();

  delay(100);

  if (buttonState = 0) {
    digitalWrite( ledPin, HIGH);
  }  
  if (buttonState = 1) {
    digitalWrite (pushButton, LOW);
  }  

}
4
  • 1
    You can format your code nicely in the Arduino IDE before pasting it here. This is clearly wrong: if (buttonState = 1). You need a double equals.
    – 6v6gt
    Feb 20, 2017 at 13:46
  • I got bored after the 400th word of the first sentence OK ...
    – Majenko
    Feb 20, 2017 at 15:21
  • ... and I think you forgot to take your Ritalin this morning ...
    – Majenko
    Feb 20, 2017 at 15:44
  • 1
    You also seem to be a noob at English. How about re-writting the question so that it makes more sense and doesn't stop mid sentence. Obvious errors - = in place of ==, you never set ledPin low, you are writing to your button input, you initalize the serial port 3 times. And seeing digital outputs called powerpin and goundpin make me worry that you've made some very bad mistakes wiring things up.
    – Andrew
    Feb 20, 2017 at 17:04

1 Answer 1

1

Use some formatter before posting it here. For example here is great one, written by KrzaQ edit: didn't noticed that was fixed by Mark

Next thing to do is keep your code clean. Three times you initialized Serial communcation. Why? You should also first initalize Serial then set pinModes into state and then put digitalWrite. It helps reading the code.

Example:

Serial.begin(value);
pinMode(x,x);
pinMode(y,x);
pinMode(z,x);
digitalWrite(x,x);
digitalWrite(y,x);
digitalWrite(z,x);

More readable, isn't it?

Next step: Pull-up and pull-down resistors... It is not thing you need to set in your code. It is hardware connection of 1 resistor, sometimes capacitor and device. There is no use for it in your device. Read about them here: Pull-up/pull-down

Next thing: You must not define your outputs as power pins and treat them like one. AVR's have limited power output from each pin and by doing so you can fry it very quickly.

If I understand your sentence correctly you need to save your angle values into variable like this (pseudo code, you have to adjust it to your case no one will write it for free):

int xvalue, yvalue, zvalue, button;
void setup(); // all setup should go here do not skip this like me here
void loop() {
  xvalue = analogRead(xpin);
  yvalue = analogRead(ypin);
  zvalue = analogRead(zpin);
  if (button == 1) {
    if (zvalue > 370)
      digitalWrite(ledPin, HIGH);
    else
      digitalWrite(ledPin, LOW);
  } else
    actionWhenPushButtonIsZero();
}

If you think about what I wrote and focus on your task I am sure that everything will work in your device. THINK first then ask next questions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.