hello i am making something that has three led strips with touch sensors near each one so one would turn green you hit the touch sensor and then another one lights up green. here is the code i have so far for the project
const byte greenLED[3] = {4, 8, 12};
const byte touchPin[1] = {31};
void setup() {
pinMode(4, OUTPUT);
pinMode(8, OUTPUT);
pinMode(12,OUTPUT);
pinMode(31,INPUT);
}
void loop() {
int touchValue = digitalRead(touchPin);
byte strip = random(3);
for (byte i = 0; i++; i < 3){
digitalWrite(greenLED[strip], (strip == i) ? HIGH : LOW);
}
}
i was helped with the code by someone from this site he suggested i put the strips in an array.
i just want to start the code with a pushbutton that can be wired to any pin in the arduino mega i am using and make it so that you have to tap the touch sensor that is attached to cycle the strips randomly. i will add two more touch sensors after i get the code to work but everytime i adjust it and add any if statements it just doesn't work. thank you to who ever can help i know this might be an easy fix but i am fairly new to electronics and coding.
As soon as touchValue is false the code turns the strips off. Tap -> flash, repeat.
for (byte i = 0; i++; i < 3){ Whoops!
Why don't you continue your previous thread?
does anyone know how to make the code where it picks a random number and lights the corresponding strip up run only once. i know the for statement makes it run over and over again but i only want it to run once but when i take the for statement out i get a ton of errors. im going to put if statement before and after the action but right now i cant get anything to work because my code knowledge isn't very good.
Use state detection: only take a random number when the touch state goes high, not when it is high, so keep track of the previous state.
Brettr12321:
does anyone know how to make the code where it picks a random number and lights the corresponding strip up run only once. i know the for statement makes it run over and over again but i only want it to run once but when i take the for statement out i get a ton of errors. im going to put if statement before and after the action but right now i cant get anything to work because my code knowledge isn't very good.
Best to put this thing aside and spend a week working through sections 1, 2, 3 and 5 of the Examples in your IDE while keeping the matching page on the Arduino main site open as online help. You don't have to do them all, some need hardware you may not have but you do want to understand the what and how of techniques (like timing or detecting pin state changes) and the why of it.
Keep a browser tab open to the Arduino Reference Page so you can quick-lookup commands and use you don't know for sure every bit of. With the compiler and syntax, "close enough" ain't.