So I have a robot that I made for class. It is meant to do like 4 things. Move forward and backward, be able to turn using a servo, turn on and off a laser, and be able to detect when the sensor is hit by a laser. It was for the most part working, is it pretty no but it was functional. The only thing that wasn't was the turning on and off the laser. I went to edit that code didn't work and when I tried again everything freaked out. So I disconnected the Arduino, got rid of that code and pull in the original code that I had saved elsewhere. However when I uploaded that to the Arduino it did the same thing it had before. After a bit of looking I figured out that everything was just reading as 1 and I don't know why. If anyone could help me figure it out and fix it I would greatly appreciate that. Also this is my first time using the forum so if I am putting this in the wrong place please let me know.
#define weaponPin 7
#define speedPin1 29
#define speedPin2 31
#define enA 11
#define Ch1 8
#define Ch2 9
#define Ch3 10
#include <Servo.h>
Servo control;
int life_count = 30;
int red = 4;
int yellow = 5;
int green = 6;
#define sensorPin 3
#define servoPin 2
int pinvalue = 1;
int var1 = 0;
void setup() {
Serial.begin(9600);
pinMode(Ch1, INPUT);
control.attach(servoPin);
pinMode(speedPin1, OUTPUT);
pinMode(speedPin2, OUTPUT);
pinMode(enA,OUTPUT);
pinMode(Ch2, INPUT);
pinMode(weaponPin, OUTPUT);
pinMode(Ch3, INPUT);
pinMode(sensorPin, INPUT);
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
}
void loop() {
pinvalue = digitalRead(sensorPin);
if(pinvalue == 1 && var1 == 0){
(life_count = life_count - 10);
var1 = 1;
}else if(pinvalue == 0 && var1 == 1){
var1 = 0;
(life_count = life_count);
}
switch(life_count){
case 30:
digitalWrite(red, HIGH);
digitalWrite(yellow, HIGH);
digitalWrite(green, HIGH);
break;
case 20:
digitalWrite(red, HIGH);
digitalWrite(yellow, HIGH);
digitalWrite(green, LOW);
break;
case 10:
digitalWrite(red, HIGH);
digitalWrite(yellow, LOW);
digitalWrite(green, LOW);
break;
case 0:
digitalWrite(red, LOW);
digitalWrite(yellow, LOW);
digitalWrite(green, LOW);
break;
default:
break;
}
int wheel= (Ch1, HIGH);
//Serial.print(wheel);
int wheel_new= map(wheel, 1120, 1980, 180, 0);
control.write(wheel_new);
int trigger= (Ch2, HIGH);
if(trigger <= 1450){
analogWrite(enA,155);
digitalWrite(speedPin1, LOW);
digitalWrite(speedPin2, HIGH);
}else if(trigger >= 1550){
analogWrite(enA,155);
digitalWrite(speedPin1, HIGH);
digitalWrite(speedPin2, LOW);
}else if(trigger <=1550 && trigger >= 1450){
analogWrite(enA,0);
digitalWrite(speedPin1, LOW);
digitalWrite(speedPin2, LOW);
}
//Serial.println(trigger);
int button= (Ch3, HIGH);
Serial.println(button);
int new_button= map(button, 600, 1600, 0, 1);
if(digitalRead(new_button)== HIGH){
digitalWrite(weaponPin, HIGH);
}else(digitalRead(new_button)== LOW);{
digitalWrite(weaponPin, LOW);
}
//Serial.println(pinvalue);
}