Random results when running this script

Hello people,

I've been trying to make a little program for my Arduino UNO R3 but i can't get it to work.
It is suppose to give me the correct values in serial but instead it sometimes randomize but sometimes the value is right i tried putting delay in the functions but didn't work

File 1 Is the Project with codes
File 2 Is the Serial output
File 3 Is the Schematic

I've tried some things but it wouldn't solve
Please help thanks! :slight_smile:

Game_Changer.ino (960 Bytes)

When the switches are down they are not connected to anything. When they are not connected to anything they are "floating". When they are floating the connected arduino pin is floating. Google can explain the rest... arduino floating input - Google Search

int p1 = 0; 

void setup() {
  Serial.begin(9600);
  pinMode(p1,INPUT);
 
}

void loop() {
  if(start == true){
   p1 = digitalRead(2); //Reads for switches is turned on

I have cut out all the similar stuff to save space.

This is all wrong. You are mixing up the numbers of the pins with the values read from those pins

It should be something like this

byte pin2 = 2; // I have changed this as it seems a bit silly to say pin1 = 2
byte p2val = 0; // it can only be 1 or 0 so you don't need an int

void setup() {
  Serial.begin(9600);
  pinMode(pin2, INPUT_PULLUP); // use INPUT_PULLUP to stop the pin from floating
 
}

void loop() {
  if(start == true){
   p2val = digitalRead(pin2); //Reads for switches is turned on

Also note the use in INPUT_PULLUP. The switch should then connect the pin to GND when the switch is pressed and the value detected when the switch is NOT pressed will be HIGH

...R

Thanks!
But Coding Badly Is there any way i can do this without buying resistors because I've tried to learn how to use them but i just can't else could you tell me which one I'm gonna use? :slight_smile:

Robin2:
It weren't helping at all I don't use resistors if you look my schematic thought because i don't know how to choose
Anyhow if i change all my codes It would just be a useless process i have a ATMEGA328P with 32KB and until now i use 3KB at the moment i just need to fix the problem so i can begin working thanks

But Coding Badly Is there any way i can do this without buying resistors...

Yes. Reread Robin2's post.

This will help... http://www.baldengineer.com/arduino-pull-ups.html

Decee1:
Anyhow if i change all my codes It would just be a useless process

If you want it to work you have no choice.

...R