I am quite a bit of a newb when it comes to programming, microcontrollers and stuff, so bear with me. I am more of an analog guy.
My problems started when I asked ChatGPT for some code (because I am such a noob) for a simple two-button code lock. The code didn't work and the AI suggested to put in some serial monitor thingies to se where things got stuck, but that didn't work either. Nothing works. Absolutely nothing! Can't see why the code shouldn't work either. No errors when compiling. So something is stuck, and so am I. Yes, I have checked it with a several types of Ardu's and even online simulators also. Tried everything I can think of. Can someone give some hints on where and why it doesn't work?
const int countPin = 2; // Pin for the count button
const int enterPin = 3; // Pin for the enter button
const int outputPin = 13; // Pin for the output
const int maxAttempts = 3; // Maximum number of attempts
const int codeLength = 4; // Length of the passcode
const int passcode[codeLength] = {1, 2, 3, 4}; // The correct passcode
int attemptCount = 0; // Number of attempts made so far
int buttonCount = 0; // Number of times the count button has been pressed
int currentDigit = 0; // The current digit being entered
void setup() {
pinMode(countPin, INPUT_PULLUP); // Set the count button as input
pinMode(enterPin, INPUT_PULLUP); // Set the enter button as input
pinMode(outputPin, OUTPUT); // Set the output as output
Serial.begin(9600); // Initialize serial communication with baud rate 9600
}
void loop() {
// Read the count button
if (digitalRead(countPin) == LOW) {
// Increment the button count
buttonCount++;
}
// Read the enter button
if (digitalRead(enterPin) == LOW) {
// Check if the current digit is correct
if (buttonCount == passcode[currentDigit]) {
// Move on to the next digit if the current one is correct
currentDigit++;
buttonCount = 0;
Serial.print(currentDigit); // Send the current digit over serial
} else {
// Reset the count and the current digit if the current one is incorrect
buttonCount = 0;
currentDigit = 0;
attemptCount++;
}
// Check if the maximum number of attempts has been reached
if (attemptCount >= maxAttempts) {
attemptCount = 0;
currentDigit = 0;
Serial.print("Max attempts reached"); // Send a message over serial
}
// Check if the entire code has been entered correctly
if (currentDigit == codeLength) {
digitalWrite(outputPin, HIGH);
currentDigit = 0;
attemptCount = 0;
Serial.print("Passcode entered correctly"); // Send a message over serial
}
}
}
You got the code posting right, but how about a quick schematic sketch, so we have some idea of the hardware configuration?
Or did ChatGPT tell you that wasn't necessary?
No, "works"/ "doesn't work " means something specific, related to the intent.
We don't know what you asked the artificial dunce, so we need to know what "works" means.
Did you see my hint about the state change detection example?
It's included in the IDE
Please explain in detail i) what is your code should to do ii) what is the code from charGPT doing and iii) what the difference between first and second
The "current" state of ChatGPT is very much in an "genius-child" state; the bot has much of the Internet knowledge captured but has no "real-world" experience to judge code quality. Spoken many, many years ago: garbage in, garbage out... GIGO.
Had you rather asked an Internet search engine the same question, your results would have been spot-on: