URGENT Help with this project (please!) Urgent!

Hi Everyone,
Completely new to this, and really don't quite follow the coding side of life - BUT am trying to sort out for a mate for a community project this weekend! Basics are: a bicycle on a training wheel, switching a video input feed to a projector when you cycle, video goes off when stopped: found an amazing project here: No TV Unless You Exercise! : 6 Steps (with Pictures) - Instructables
Have cobbled together and it kind of works, but. need the switching to be alot simpler without the timers and the LED and sounder - literally just so that when pedalling it switches relay on, stop pedalling it switches off.
Hers' the code verbatim at the moment. MANY THANKS f you can help me!?

int ledpin = 13; //activates arduino light when there's a detection
int ledpin2 = 11; //activates external LED or buzzer when there's a detection
int outputpin = 3; //pin the relay control is attached to
int sensorpin = 12; //pin the detector is attached to
int warnpin = 4; //pin used to activate warning LED or buzzer
int counter = 0; //countdown since last detection
int counter2 = 0; //countdown since last non-detection

//change the next 2 settings to adjust the timer intervals
int countmax = 10000; //maximum level of countdown timer. 1000 = 1 second
int countwarn = 2000; //when to turn on warning pin. 1000 = 1 second

void setup()
{
pinMode(ledpin, OUTPUT);
pinMode(ledpin2, OUTPUT);
pinMode(outputpin, OUTPUT);
pinMode(warnpin, OUTPUT);
pinMode(sensorpin, INPUT);
counter = countmax;
counter2 = countmax;
//any line with "serial" is for debugging and should be commented out if not needed because it will slow the clock down
//Serial.begin(9600);
//Serial.print("count: ");
//Serial.println(counter);
//Serial.print("count2: ");
//Serial.println(counter2);
}

void loop()
{
if ( digitalRead(sensorpin) ) //what to do when there is a non-detection
{
digitalWrite(ledpin,LOW );
digitalWrite(ledpin2,LOW );
counter = counter - 10;
counter2 = countmax;
}
else //what to do when there is a detection
{
digitalWrite(ledpin,HIGH);
digitalWrite(ledpin2,HIGH);
counter = countmax;
counter2 = counter2 - 10;
}

//serial output lines for debugging
//Serial.print("count: ");
//Serial.println(counter);
//Serial.print("count2: ");
//Serial.println(counter2); 
//Serial.println("-----------");

//sound a warning if either counter is above 0 but under the "countwarn" level
if ( counter > 0 && counter < countwarn || counter2 >0 && counter2 < countwarn)
 {
  digitalWrite( warnpin,HIGH );
 }      
 else    
   {
    digitalWrite( warnpin,LOW );
   }  

//turn off the output if either counter drops below 0
//these settings are for an "active low" relay.  Switch "HIGH" and LOW for an active high output
if ( counter < 0 )
  {
    digitalWrite(outputpin,HIGH );
    counter = 0;
  }
 else    
   if ( counter2 < 0)
    {
    digitalWrite(outputpin,HIGH );
    counter2 = 0;
    }
    else  
      {
       digitalWrite( outputpin,LOW );
      }  

delay(10); // wait time (miliseconds) before going again

}

Welcome to the forum

If you want help, urgent or not, please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

Please post your full sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It is also helpful to post full error messages in code tags as it makes it easier to scroll through them and copy them for examination

1 Like

@UKHeliBob
It's interesting how those with the highest urgency (urgent in the title twice is a little over the top), are those with the lowest likelihood of following instructions that are intended to improve the speed and accuracy of the help they are requesting.

Life is strange, and most human life is most strange.

@wonkytv You really do need to sit back, read the help guideline pointed to, and get together some information, so that we can help you expeditiously. Otherwise, this thread will likely devolve into a session of 20 questions, by the end of which you'll likely be so frustrated you'll quit in disgust, thinking we're the meanies, when what we need to help you is right in front of you - but not in front of us.
Best of luck.

2 Likes

Here is the original code:

int ledpin = 13;     //activates arduino light when there's a detection
int ledpin2 = 11;    //activates external LED or buzzer when there's a detection
int outputpin = 3;   //pin the relay control is attached to
int sensorpin = 12;  //pin the detector is attached to
int warnpin = 4;     //pin used to activate warning LED or buzzer
int counter = 0;     //countdown since last detection
int counter2 = 0;    //countdown since last non-detection

//change the next 2 settings to adjust the timer intervals
int countmax = 10000;  //maximum level of countdown timer. 1000 = 1 second
int countwarn = 2000;  //when to turn on warning pin. 1000 = 1 second

void setup() {
  pinMode(ledpin, OUTPUT);
  pinMode(ledpin2, OUTPUT);
  pinMode(outputpin, OUTPUT);
  pinMode(warnpin, OUTPUT);
  pinMode(sensorpin, INPUT);
  counter = countmax;
  counter2 = countmax;
  //any line with "serial" is for debugging and should be commented out if not needed because it will slow the clock down
  //Serial.begin(9600);
  //Serial.print("count: ");
  //Serial.println(counter);
  //Serial.print("count2: ");
  //Serial.println(counter2);
}

void loop() {
  if (digitalRead(sensorpin))  //what to do when there is a non-detection
  {
    digitalWrite(ledpin, LOW);
    digitalWrite(ledpin2, LOW);
    counter = counter - 10;
    counter2 = countmax;
  } else  //what to do when there is a detection
  {
    digitalWrite(ledpin, HIGH);
    digitalWrite(ledpin2, HIGH);
    counter = countmax;
    counter2 = counter2 - 10;
  }

  //serial output lines for debugging
  //Serial.print("count: ");
  //Serial.println(counter);
  //Serial.print("count2: ");
  //Serial.println(counter2);
  //Serial.println("-----------");

  //sound a warning if either counter is above 0 but under the "countwarn" level
  if (counter > 0 && counter < countwarn || counter2 > 0 && counter2 < countwarn) {
    digitalWrite(warnpin, HIGH);
  } else {
    digitalWrite(warnpin, LOW);
  }

  //turn off the output if either counter drops below 0
  //these settings are for an "active low" relay.  Switch "HIGH" and LOW for an active high output
  if (counter < 0) {
    digitalWrite(outputpin, HIGH);
    counter = 0;
  } else if (counter2 < 0) {
    digitalWrite(outputpin, HIGH);
    counter2 = 0;
  } else {
    digitalWrite(outputpin, LOW);
  }

  delay(10);  // wait time (miliseconds) before going again
}

If you simply do not connect an external LED or buzzer, the code will do exactly what you want - just turn on/off depending on the detector.

where is countwarn both defined and it's value set?

Why is this urgent?

1 Like

urgent

Only 4.5 working days to go.

1 Like

This looks like a school project to me, am I correct?

This is such an urgent project that the author doesn't even have time to discuss it with us - he's just waiting for a ready-made solution.

hi all, thank you so much for your replies, granted I'm a total newbie, so apologies, for not knowing the rules etc. bit of a fix, that's all, the original version that was very kindly reposted correctly blh64 thnanks, doesn't seem to simply turn on off the relay, but it has timers involved, count down etc. I really just need this to either be on or off when cycling, yes a school project of sort I suppose, I'm learning eh! thanks all again

To learn something it would be better for you try to modify the code yourself.

thank you, have just learnt something new ;0) apologies

thank, have tried that, but seem that the timers and other options (which I do not understand) don't quite behave... thanks though.

urgent, cos it is needed to work this weekend, simple no?

anyway, any help would be greatly appreciated, kids are looking forward to trying out cycling 'through france', that's the idea anyway... cheers good night for now ;0)

You don't ask for help, you are waiting for a ready-made code. Sorry, but the forum not works this way.
Please show what you did yourself.

that's what I have been doing all day... and not really getting anywhere, that's why I cam to the forum to find out how to do this, not really have a confab about what I should be doing... do you understand? Please leave this conversation if you don't want to help out thank you.

const int sensorPin = 12;  // TCRT5000 digital output
const int relayPin = 3;    // Relay control pin

int counter = 0;            // Counts down after last detection
const int countMax = 1000;  // Adjust to control how long relay stays ON (1000 ~ 1 second)

void setup() {
  pinMode(sensorPin, INPUT);
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW);  // relay OFF initially
  Serial.begin(9600);
  counter = 0;
}

void loop() {
  // Sensor detects white patch (active LOW on many modules)
  if (digitalRead(sensorPin) == LOW) {
    counter = countMax;            // Reset counter whenever white patch detected
    digitalWrite(relayPin, HIGH);  // Relay ON
    Serial.println("Wheel moving → Relay ON");
  } else {
    // Countdown: relay stays ON for a short time even if sensor reads black
    if (counter > 0) {
      counter--;
      digitalWrite(relayPin, HIGH);  // Keep relay ON
    } else {
      digitalWrite(relayPin, LOW);  // Relay OFF
      Serial.println("Wheel stopped → Relay OFF");
    }
  }

  delay(1);  // Small delay for stability
}

thanks for that, really helpful

1 Like