Okay first of all hello friends, it's my first time posting here, yes I have read the required read before posting reading material and I have looked in a great deal of other sources but I can't seem to find what I am looking for.
So I basically have 2 questions, 1st what is wrong with my code (I think in logical coding reasoning there must be something wrong) I am not a programmer so I only understand the basics so I cant seem to find what I'm doing wrong, the second question is are the things that I could have written more smartly and if sow how?
What I am trying to do is, I'm trying to make a moisture checker for plants (with some tinkering to go it's supposed to work on 4 different plants) and the idea is that when I push the button it goes and turns on the LED for the first plant, then checks the moisture sensor and on the basis of that it moves the pointer on a servo motor. It does the exact same if I push the button again but it's supposed to light up a different LED and I'll later on put different types of numbers required for plants. So I push again and it repeats almost the same process but for a different plant with different numbers and a different LED. If I pushed the button 4 times it should go back to the first mode again. I wrote this code but it doesn't seem to work:
#include <Servo.h>
// servo
int positie = 0;
Servo servo_5;
// water sensor
int sensorPin = A1;
int sensorValue = 0;
//counts number of times button pressed
int counter = 0;
// lampjes
const int LED1 = 8; //number of led pin
const int LED2 = 7;
const int LED3 = 6;
const int LED4 = 4;
// button
const int buttonPin = 2; // the number of the pushbutton pin
int buttonState = 0;
void setup() {
// monitor
Serial.begin(9600);
// watersensor
pinMode(sensorPin, INPUT);
//button
pinMode(buttonPin, INPUT);
//servo
servo_5.attach(5);
//led
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
//is this right??
counter = digitalRead(buttonPin);
if (buttonState = HIGH) {
(counter ++ ) ; // every time button is pushed buttonState adds up
if ( counter == 5) { //reset counter
( counter = 0); }
}
if (counter = 1) {
digitalWrite(LED1, HIGH);
positie = map(sensorValue, 0, 950, 0, 180);
servo_5.write(positie);
Serial.println(sensorValue);
delay(10); // Delay a little bit to improve simulation performance
}
if (counter = 2) {
digitalWrite(LED2, HIGH);
positie = map(sensorValue, 0, 950, 0, 180);
servo_5.write(positie);
Serial.println(sensorValue);
delay(10); // Delay a little bit to improve simulation performance
}
if (counter = 3) {
digitalWrite(LED3, HIGH);
positie = map(sensorValue, 0, 950, 0, 180);
servo_5.write(positie);
Serial.println(sensorValue);
delay(10); // Delay a little bit to improve simulation performance
}
if (counter = 4) {
digitalWrite(LED1, HIGH);
positie = map(sensorValue, 0, 950, 0, 180);
servo_5.write(positie);
Serial.println(sensorValue);
delay(10); // Delay a little bit to improve simulation performance
}
}
I work with a Arduino Uno with a grove shield, grove moisture sensor, grove led, a normal servo motor and a grove button.
It's very unlikely that at least on the Arduino things aren't wired correctly so it's really about the code