I am using Processing language to sketch a rectangle that grows in size with time. Following code is not giving any output.
void setup()
{
size(900,900);
}
void draw()
{
int edge=100;
for(int i=0;i<300;i++)
{
delay(100);
edge++;
rect(100,100,edge,edge);
}
}
I suspect having wrongly used delay() function.
Here is one such "roll your own" delay method which is good for most purposes. Just change the values passed into the delay method to alter the timing. This just outputs "start" and "end" roughly every 2 seconds for example.
void draw()
{
System.out.println("start");
delay(2000);
System.out.println("end");
delay(2000);
}
void delay(int delay)
{
int time = millis();
while(millis() - time <= delay);
}
I recommend rolling your own delay system using the millis() function.
Have a look at this example.
With processing, the screen does not get refreshed until the program flow reaches the end of draw()
Try the following:
void setup()
{
size(900,900);
frameRate(10);
}
int edge = 100;
void draw()
{
edge++;
rect(100,100,edge,edge);
}
Related
I'm trying to blink a led with analog sensor between 8 and 40 times a minutes
I have try this code but I realize that I had to convert the valorSensor to time. How is the way to do it?
int led = 13;
int pinAnalogo = A0;
int analogo;
int valorSensor;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
analogo = analogRead(pinAnalogo);
valorSensor = map(analogo, 0, 1023, 4, 80);
digitalWrite(led, HIGH);
delay(valorSensor);
digitalWrite(led, LOW);
delay(valorSensor);
}
The problem here is not so much the code but biology. To see a blinking (and not just a flicker you will need times from 250ms and up) 24 frames/sec are perceived as motion so to get a "blink" you might start with 4 frames/sec (=250ms)
So my proposal is a blink without delay as function and a tuning parameter (blinkSpeedMultiplyer) for testing
/* Blink without Delay as function
Turns on and off a light emitting diode (LED) connected to a digital pin,
without using the delay() function. This means that other code can run at the
same time without being interrupted by the LED code.*/
// constants won't change. Used here to set a pin number:
const int blinkSpeedMultiplyer = 50; // very low
const int ledPin = 13;
const int pinAnalogo = A0;
// Variables will change:
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long startTime = millis(); // will store last time LED was updated
unsigned long blinkTime; // interval at which to blink (milliseconds)
int analogo;
int valorSensor;
int ledState = LOW; // ledState used to set the LED
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
analogo = analogRead(pinAnalogo);
valorSensor = map(analogo, 0, 1023, 4, 80);
blinkLed(valorSensor); // call the function
}
// Function to blink without blocking delay
void blinkLed(int inValue) {
blinkTime = blinkSpeedMultiplyer * inValue;
if (millis() - startTime >= blinkTime) {
// save the last time you blinked the LED
startTime = millis();
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
I was just messing around with the while loop, testing it vs the for loop, when this graphical glitch occurred. This happens on an inconsistent basis, so I do refresh it to get results, but occasionally the screen will be split into horizontal lines, each with their different color. Why is this occurring?
int i = 0;
void setup() {
size(400, 400);
}
void draw() {
while(i < 1) {
background(random(255),random(255),random(255));
}
println("yes");
}
void mousePressed() {
i += 1;
}
I'm using a microcontroler PIC 32 in a diligent cerebot Mx4cK.
I have switch implemented on my protoboard and I want to turn on a led after the switch it's activated, then this led have to be in this state for 2 seconds and then have to be off for another 3 seconds and start all over again (on->2 seconds off->3 seconds)
This is my code so far, I think it's missing one condition but I can't find it... can you help me?
const int led=PIN_LED1;
const int pinSwitch1=16;
void setup()
{
pinMode(pinSwitch1,INPUT);
pinMode(led,OUTPUT);
digitalWrite(led,LOW);
}
void loop()
{
unsigned long actual_time=millis();
static unsigned long cicle_time=0;
static unsigned long off_time=0;
static int switch_state1=0;
switch_state1=digitalRead(pinSwitch1);
if (switch_state1==HIGH)
{
if((actual_time-cicle_time)<5000)
{
digitalWrite(led,HIGH);
cicle_time=actual_time;
}
if((actual_time-off_time)>2000)
{
digitalWrite(led,LOW);
off_time=actual_time;
}
}
else
{
digitalWrite(led,LOW);
}
}
Actually my code, blincks for 2 seconds and it's not consider the 3 seconds that it has to be off.
[This is my new code, I missing an initial condition to light for the first time]
const int led=PIN_LED1;
const int pinSwitch1=16;
void setup()
{
pinMode(pinSwitch1,INPUT);
pinMode(led,OUTPUT);
digitalWrite(led,LOW);
}
void loop()
{
unsigned long actual_time=millis();
static unsigned long cicle_time=0;
static unsigned long off_time=0;
static int switch_state1=0;
static int cicle_on=0;
switch_state1=digitalRead(pinSwitch1);
if (switch_state1==HIGH)
{
if((actual_time-cicle_time)>5000)
{
digitalWrite(led,HIGH);
cicle_time=actual_time;
cicle_on=HIGH;
}
}
else
{
digitalWrite(led,LOW);
}
if((actual_time-off_time)>2000)
{
digitalWrite(led,LOW);
off_time=actual_time;
cicle_on=LOW;
}
}
generic code debug is off-topic here
and you even do not specify what this does instead of what it should do
you are writing to LED every cycle
which slows things down
when you add more stuff then it could give you many head ages later
use absolute time instead of relative for your planned events and update only when needed
static unsigned long time_LED_on =0xFFFFFFFF;
static unsigned long time_LED_off=0xFFFFFFFF;
//...
if ((switch_state1==HIGH)&&(time_LED_on!=0xFFFFFFFF)) // init on switch toggle only would be better in interrupt
{
time_LED_on =actual_time;
time_LED_off=actual_time+2000;
}
if (switch_state1==LOW ) // stop if switch off also would be better in interrupt
{
time_LED_on =0xFFFFFFFF;
time_LED_off=0xFFFFFFFF;
}
// handle LED event
if (time_LED_on >=actual_time) { digitalWrite(led,HIGH); time_LED_on +=5000; }
if (time_LED_off>=actual_time) { digitalWrite(led,LOW ); time_LED_off+=5000; }
beware that time can overflow you can handle it by
if (min(all_times) > max) all_times -= max;
dont know how many bits your platform have if it is not 32 then change the 0xFFFFFFFF acordingly
This should solve your problem:
const int led=PIN_LED1;
const int pinSwitch1=16;
unsigned long cicle_time=0;
void setup()
{
pinMode(pinSwitch1,INPUT);
pinMode(led,OUTPUT);
digitalWrite(led,LOW);
}
void loop()
{
unsigned long actual_time=0;
static int switch_state1=0
switch_state1=digitalRead(pinSwitch1);
digitalWrite(led,LOW);
while(switch_state1==HIGH)
{
digitalWrite(led,HIGH);
cicle_time=millis();
while((millis()-cicle_time)!>=2000)
{
}
cicle_time=millis();
digitalWrite(led,LOW);
while((millis()-circle_time)!>=3000)
{
}
}
}
I created a simple delay function for my sketch and tried to use it, but it seems as if the rendering stops i.e., there is simple a grey screen and then everything is rendered all at once.
Could someone please tell me where I am going wrong? What exactly is happening?
Also how are draw() and setup() defined internally? I understand that setup() is a one time render and draw() like an infinite loop.
Code:
void delay(int delay)
{
int time = millis();
while(millis() - time <= delay);
}
void setup(){
size(600,400);
smooth();
}
void draw(){
background(0); //black
delay(1000);
fill(255); //white
ellipse(width/2, height/2, 300, 300);
delay(1000);
}
Also how are draw() and setup() defined internally? I understand that setup() is a one time render >and draw() like an infinite loop.
The thing is that draw() only renders a frame at the end of each execution, so stoping it is usually not a good idea.
http://wiki.processing.org/w/I_display_images_in_sequence_but_I_see_only_the_last_one._Why%3F
If you want to time things use booleans as flags for when things should happen.
check those also:
http://wiki.processing.org/w/What_are_setup()_and_draw()%3F
http://wiki.processing.org/w/How_do_I_display_a_message_for_a_few_seconds%3F
If you want delay, use a Thread() object, like the following code, to run the delays in a separate thread:
void delay(int delay)
{
try
{
Thread.sleep(delay);
}
catch(Exception EX)
{
}
}
void setup(){
size(600,400);
smooth();
thread("sleepy"); // the function sleepy will be started in a new thread
}
void draw(){
// draw() does nothing here except refresh the screen
}
void sleepy()
{
while(true)
{
background(0); //black
delay(1000);
fill(255); //white
ellipse(width/2, height/2, 300, 300);
delay(1000);
}
}
Additionally, the above delay method, Thread.sleep(), does not consume an entire CPU core as it runs (The millis() method is akin to a temporary infinite loop).
Tested on Processing 2.2.1, Windows 7 professional.
My aim is to create a for loop that iterates through numbers and once it reaches the maximum, it stops printing. So far I managed to create a piece of code that stops printing the x but it keeps printing zeroes. How can I stop Serial.print() function to be executed once the iteration reached the maximum value?
int x;
boolean f = false;
void setup(){
Serial.begin(9600);
}
void loop(){
for(x=0;x<8;x++){
Serial.println(x);
delay(300);
if(x==7){
f = true;
}
if(f){
break;
}
}
}
Something like below should serve. Btw, I like to name my vars something meaningful to avoid potential confusion and make the code more intelligible.
(In general you are better off posting questions to the Arduino forum. More traffic and more knowledgeable/helpful people == more likelihood of getting an answer.)
int current;
int limit;
boolean complete;
void setup(){
Serial.begin(9600);
current = 0;
limit = 8;
complete = false;
}
void loop(){
if (!complete){
while (true){
Serial.println(current);
current++;
if (current >= limit){
complete = true;
break;
}
delay(300);
}
}
}
The key is the word loop - that function is called repetitively!
If you want something to happen once, do it in setup(), or (as another answer suggests), have a flag to keep track of the fact that you've done it already.
Another way (since x is global) would be:
void loop() {
if (x < 8) {
Serial.println(x);
x++;
}
}
or, getting rid of the global variable:
void loop() {
static int x = 0;
if (x < 8) {
Serial.println(x);
x++;
}
}
int x;
boolean f = false;
void setup(){
Serial.begin(9600);
}
Very similar to your code, just put the print statement in the if statement and you're set.
void loop(){
for(x=0;x<8;x++){
if(!f) {
Serial.println(x);
}
delay(300);
if(x==7){
f = true;
}
}
}